Python IndentationError - How to refactor?
Question: I am doing a [Project Euler](https://projecteuler.net) question for
programming practice in order to self-teach myself. I know perfectly well
_how_ to do the question mathematically, _as well as_ how to do it
programmatically.

However, I have to have come up with some insane code to do it; 100 nested
loops and Python hilariously raises this error, and probably rightfully so, on
100 levels of indentation:

    
    
    IndentationError: too many levels of indentation
    
    
    
    tally = 0
    ceiling = 100
    for integer_1 in range(0, 100, 1):
        for integer_2 in range(0, 100 - integer_1, 2):
            for integer_3 in range(0, 100 - integer_1 - integer_2, 3):
                for integer_4 ....
                    for integer_5 ....
                        etc.
                            etc.
                                all the way to integer_100
    

I have looked through google for solutions but this issue is so rare it has
almost no literature on the subject and I could only find this other stack
overflow question ( [Python IndentationError: too many levels of
indentation](http://stackoverflow.com/questions/8439572/python-
indentationerror-too-many-levels-of-indentation) ) which I could not find much
useful in for my question.

My question is - is there a way to take my solution and find some workaround
or refactor it in a way that has it work? I am truly stumped.

**EDIT:**

Thanks to nneonneo's answer, I was able to solve the question. My code is here
just for future reference of people looking for ways to properly refactor
their code.

    
    
    from time import time
    t = time()
    count_rec_dict = {}
    
    # for finding ways to sum to 100
    def count_rec(cursum, level):
        global count_rec_dict
    
        # 99 is the last integer that we could be using,
        # so prevent the algorithm from going further. 
        if level == 99:
            if cursum == 100:
                return 1
            else:
                return 0
    
        res = 0
    
        for i in xrange(0, 101-cursum, level+1):
    
            # fetch branch value from the dictionary
            if (cursum+i, level+1) in count_rec_dict:
                res += count_rec_dict[(cursum+i, level+1)]
    
            # add branch value to the dictionary
            else:
                count_rec_dict[(cursum+i, level+1)] = count_rec(cursum+i, level+1)
                res += count_rec_dict[(cursum+i, level+1)]        
    
        return res}
    
    print count_rec(0, 0)
    print time() - t
    

which runs in an astonishing 0.041 seconds on my computer. WOW!!!!! I learned
some new things today!


Answer: A recursive solution should do nicely, though I'm certain there is an entirely
different solution to the problem that doesn't require this kind of
manipulation.

    
    
    def count_rec(cursum, level):
        if level == 100:
            return 1
        res = 0
        for i in xrange(0, 100-cursum, level+1):
            res += count_rec(cursum+i, level+1)
        return res
    
    print count_rec(0, 0)
    

Interestingly enough, if you _memoize_ this function, it will actually have a
reasonable running time (such is the power of _dynamic programming_). Have
fun!

