Quote Originally Posted by RedfishBluefish View Post
One thing: the range() function is (in mathy words) inclusive-exclusive [a,b)... in Pythony words, range(0, x) returns [0, 1, 2, ..., x-2, x-1]. So you'll want to change eg. "for a in range(0,200)" -> "for a in range(0, 200+1)" to get those cases where any one coin makes up the whole thing.

This actually might explain the problem with your second code... if a is the number if 1p coins that need to be added to get to £2, and you use range(0, a), then it will only go up to a-1 and never actually reach the value a, and so never obtain £2. Similarly of course for the outer loops. So you need to add one to each of those ranges as well.

That probably answers your question.

Just because I have to put my own ideas in , you can actually eliminate the final inner loop in the second version completely. If r >= 0 then clearly you can add exactly r 1p coins to make £2, so that's one combination done .

Another trick is using xrange() instead of range() which is exactly the same but returns a generator instead of a list, probably speeding things up a bit since you don't have to allocate lots and lots of lists.
Thanks
I figured the r thingey today, too.
Thanks for your advice!