Tax Brackets

January 26, 2021

We represent the tax brackets as a list of pairs, arranged from high to low:

(define brackets '((100000 0.4) (30000 0.25) (10000 0.1)))

Then the tax calculation is a simple recursion:

(define (tax income brackets)
   (if (null? brackets) 0
     (+ (* (max (- income (caar brackets)) 0) (cadar brackets))
        (tax (min income (caar brackets)) (cdr brackets)))))

Then the tax calculation is a simple recursion:

 > (tax 123456 brackets)
 28882.4 

You can run the program at https://ideone.com/T62CTT.

Pages: 1 2

4 Responses to “Tax Brackets”

  1. James Smith said

    Perl

    sub tax {
      return ( ( $_[0] >= 10_000  ? 2*($_[0]-10_000)  : 0 ) 
             + ( $_[0] >= 30_000  ? 3*($_[0]-30_000)  : 0 ) 
             + ( $_[0] >= 100_000 ? 3*($_[0]-100_000) : 0 )
             ) / 20; 
    }
    
  2. James Smith said

    Forgot to say – the trick here is to only add the additional tax per bracket – so about 10K is 10%, above 30K is then (25-10)% = 15% and about 100K is then (40-25)% = 15% as well…

  3. Kevin said

    A solution in Racket:

    (define (taxbrk in (brk '(100000 30000 10000 0)) (tax '(0.40 0.25 0.10 0)) (out 0))
      (cond
        ((zero? in) (display (real->decimal-string out)))
        ((> in (car brk)) (taxbrk (car brk) (cdr brk) (cdr tax) (+ out (* (car tax) (- in (car brk))))))
        (else (taxbrk in (cdr brk) (cdr tax) out))))
    

    Solution in use:

    > (taxbrk 123456)
    28882.40
    > (taxbrk 0)
    0.00
    > (taxbrk 10000)
    0.00
    > (taxbrk 30000)
    2000.00
    > (taxbrk 100000)
    19500.00
    > (taxbrk 100200)
    19580.00
    
  4. Daniel said

    Here’s a solution in Python.

    from decimal import Decimal
    
    def tax(income):
        output = Decimal(0)
        for threshold, rate in (100_000, '.40'), (30_000, '.25'), (10_000, '.10'):
            if income >= threshold:
                output += Decimal(rate) * (income - threshold)
                income = threshold
        return output
    
    print(tax(123_456))
    

    Output:

    28882.40
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: