Any, problem 31 - I remember this one giving me problems my first run through project Euler, but I am not entirely sure why, pretty simple solution - I decided to use Javascript in order to take it easy, but I would not be surprised if I resolve this with something far more ridiculous later.
amounts = new Array(1, 2, 5, 10, 20, 50, 100); ways = 1; //accounts for the 2 pound coin function helper(amountIndex, inheritedAmount) { for (var i = 0; i <= 200; ++i) { current = inheritedAmount + (i * amounts[amountIndex]); if (current > 200) { break; } else if (current == 200) { ways++; break; } else if (amountIndex < amounts.length - 1) { helper(amountIndex + 1, current); } } } function euler31() { helper(0,0); alert(ways); }
No comments:
Post a Comment