generated at
入力した税率となる所得を返すプログラム

目的:(Xにかかる税金)/X = 指定税率 を解く

アイデア
平均税率テーブルを計算しておく
195万円以下の場合29.25/195
195-330万円の場合(29.25+27)/330
330-695万円の場合(29.25+27+109.5)/695
695-900万円の場合(29.25+27+109.5+67.65)/900
900-1800万円の場合(29.25+27+109.5+67.65+387)/1800
1800万円-4000万円:(29.25+27+109.5+67.65+387+1100)/4000
4000万円以上の場合(29.25+27+109.5+67.65+387+1100+(X-4000)*0.55)/X
与えられた税率が出てきたら、テーブルを見てどの範囲に収まるかを確認して計算する
例:
0.15が与えられた場合、テーブルに一致するのは195万円以下なので195万円
0.20が与えられた場合、330-695万円の間なので(29.25+27+(x-330)/xを解く


py
from sympy import symbols, Eq, solve # 入力した税率となる所得の上限を返す関数 def calculate_income_for_tax_rate(tax_rate): # Tax brackets (income, rate) tax_brackets = [ (1950000, 0.15), (3300000, 0.2), (6950000, 0.3), (9000000, 0.33), (18000000, 0.43), (40000000, 0.5) ] # Initialize cumulative tax and previous upper limit cumulative_tax = 0.0 prev_limit = 0.0 # Initialize symbolic variable x = symbols('x') # Check for rates below the minimum tax rate if tax_rate < 0.15: return None # Special case for the first tax bracket if tax_rate == 0.15: return 1950000.0 # Loop through each tax bracket to find the range where the given average tax rate falls for upper_limit, rate in tax_brackets: prev_cumulative_tax = cumulative_tax # Calculate the total tax for incomes up to the upper limit of this bracket cumulative_tax += (upper_limit - prev_limit) * rate # Calculate the average tax rate for incomes up to the upper limit of this bracket avg_tax_rate = cumulative_tax / upper_limit # Check if the given tax rate falls within this bracket if avg_tax_rate >= tax_rate: # Solve for the income that would result in the given average tax rate equation = Eq((prev_cumulative_tax + (x - prev_limit) * rate) / x, tax_rate) income = solve(equation) # Return the income if a valid solution exists if income and income[0] > 0: return float(income[0]) # Update the previous upper limit for the next iteration prev_limit = upper_limit # If we get here, the tax rate is above 50% and should fall into the 55% bracket equation = Eq((cumulative_tax + (x - 40000000) * 0.55) / x, tax_rate) income = solve(equation) if income and income[0] > 0: return float(income[0]) # If no solution was found, return None return None # Testing the updated function with an average tax rate of 0.5 (50%) calculate_income_for_tax_rate(0.5)

verify.py
# 入力値が正しいかの検算コード def verify_calculated_income(calculated_income): tax_brackets_verification = [ (1950000, 0.15), (3300000, 0.2), (6950000, 0.3), (9000000, 0.33), (18000000, 0.43), (40000000, 0.5) ] cumulative_tax_verification = 0.0 prev_limit_verification = 0.0 for upper_limit, rate in tax_brackets_verification: if calculated_income > upper_limit: cumulative_tax_verification += (upper_limit - prev_limit_verification) * rate else: cumulative_tax_verification += (calculated_income - prev_limit_verification) * rate break prev_limit_verification = upper_limit # Only apply the 55% rate for incomes above 40,000,000 JPY if calculated_income > 40000000: cumulative_tax_verification += (calculated_income - 40000000) * 0.55 average_tax_rate_verification = cumulative_tax_verification / calculated_income return cumulative_tax_verification, average_tax_rate_verification