然後稍修改了下。自己要手動算這個挺麻煩。
假定你公司的401k計劃是最多match到6% of your salary based on your contribution.
目標是 (1)最大化拿到公司的match (2)之後最大化Roth contribution
跑程序時輸入 (1)公司match的比例,一般是50%或100%,(2)你的年薪
def calculate_401k_contributions(annual_salary, company_match_percentage=0.5, max_company_match_percentage=0.06, total_401k_limit=23000):
max_pre_tax_contribution = annual_salary * max_company_match_percentage
max_pre_tax_contribution = min(total_401k_limit, max_pre_tax_contribution)
company_match = max_pre_tax_contribution * company_match_percentage
remaining_pre_tax_limit = total_401k_limit - company_match
remaining_pre_tax_limit = max(0, remaining_pre_tax_limit)
roth_401k_contribution = min(remaining_pre_tax_limit, total_401k_limit - max_pre_tax_contribution)
pre_tax_contribution_to_maximize_match = min(max_pre_tax_contribution, total_401k_limit - roth_401k_contribution)
remaining_pre_tax_contribution = max_pre_tax_contribution - pre_tax_contribution_to_maximize_match
additional_roth_401k_contribution = min(remaining_pre_tax_contribution, roth_401k_contribution)
pre_tax_contribution = pre_tax_contribution_to_maximize_match + additional_roth_401k_contribution
return pre_tax_contribution, roth_401k_contribution
# usage
company_match_percentage = input("Enter your company 401k match % (e.g. 50%): ")
if company_match_percentage.isdigit():
company_match_percentage = float(company_match_percentage)
if company_match_percentage > 1:
company_match_percentage = company_match_percentage / 100
else:
company_match_percentage = float(company_match_percentage.strip('%')) / 100.0
annual_salary = float(input("Enter your annual salary: "))
pre_tax, roth = calculate_401k_contributions(annual_salary, company_match_percentage)
print(f"Annual Salary: ${annual_salary}")
print(f"Pre-tax 401k Contribution: ${pre_tax:.2f}")
print(f"Roth 401k Contribution: ${roth:.2f}")