Salary Calculation Based on 401K Contribution Plus Taxes – April 05, 2024

I was faced with an interesting problem yesterday of trying to calculate the optimal salary for someone who wants to contribute all of their income to their 401K.  And "optimal" is defined as "near as possible to the deductible maximum and not have any half penny or rounding issues per month or for the quarterly social security and medicare reports".  In previous years (especially last year) it was trivial to get the right number, and I didn't have to think too much.  I poked around guessing some numbers but kept getting fractional payments when multiplying the tax rates, so ended up writing a script to do it, and got an excuse to do it in Python, since I've been trying to learn it, but often don't have reasons to use it. (not that this script is all that interesting from a Python perspective).  As my first Java instructor said, "it looks like you took a C program and then renamed it .java and modified it until the compiler didn't complain."  And I had to admit that he was right...

#!/bin/python3
MAX_401K=23000
SOCIAL_SECURITY_RATE=0.062
MEDICARE_RATE=0.0145
TEST_RANGE=200
MONTHLY_NET=int(MAX_401K/12)
MONTHLY_GROSS = int(MONTHLY_NET / (1 - SOCIAL_SECURITY_RATE - MEDICARE_RATE))

for gross in range(MONTHLY_GROSS,MONTHLY_GROSS-TEST_RANGE, -1):
  social=gross*SOCIAL_SECURITY_RATE
  if(social*100 == int(social*100)):
    medicare=gross*MEDICARE_RATE
    if(medicare*100 == int(medicare*100)):
      net=gross-medicare-social
      print("gross:",gross,"net:",net," social:",social,"  medicare:",medicare)
      break

Hope this helps someone else, and maybe I'll even remember I wrote this next year and not calculate it manually...