Wednesday, March 04, 2015

federal tax

bracket:
    limit
    percentage

ex: 
brackets = [
    { limit:  50k, percentage: 10% },
    { limit: 100k, percentage: 15% },
    { limit: 150k, percentage: 20% },
    { limit: 200k, percentage: 25% },
    { limit: 250k, percentage: 30% },
    { limit: 300k, percentage: 35% },
    { limit:    ∞, percentage: 40% },
]

federalTax(taxable, brackets):
    tax = 0
    prevBracketLimit = 0
    foreach bracket in brackets:
        if taxable <= prevBracketLimit:
            break
        delta = bracket.limit - prevBracketLimit
        if taxable < bracket.limit:
            delta = taxable - prevBracketLimit
        tax = tax + delta * bracket.percentage
        prevBracketLimit = bracket.limit
    return tax


[Hat tip to tax season]

No comments:

Post a Comment