Friday, August 23, 2013

Problem 31 - Javascript

I got a little bit of the Shakespeare compiler done today, but I decided to take a break and solve a problem in a comfortable language - the Shakespeare compiler doesn't seem too hard to write, the most annoying part will be working with all of the silly wordlists of "neutral adjectives," "bad adjectives," etc, etc.

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