Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
Created December 3, 2011 16:25
Show Gist options
  • Select an option

  • Save jakedobkin/1427499 to your computer and use it in GitHub Desktop.

Select an option

Save jakedobkin/1427499 to your computer and use it in GitHub Desktop.
Euler 31
#!/usr/local/bin/node
// our basic method here is to start with the largest coin and count down to the smallest
// each time you use up a coin (ie. that loop gets to zero) you're still checking the next
// smallest coin, down to the smallest one. there is probably a way to write this recursively
// instead of copying out the loop 8 times, but i don't know recursive functions yet.
// remove the comments if you want an actual enumeration of each set.
goal = 200;
count = 0;
for (a=1;a>=0;a--)
{
atotal = goal - a*200;
if (atotal == 0)
{
// console.log(a+" 200s is equal to 200");
count +=1;
}
if (atotal > 0)
{
for (b=2;b>=0;b--)
{
btotal = atotal - b*100;
if (btotal == 0)
{
// console.log(a+" 200s plus "+b+ " 100s is equal to 200");
count +=1;
}
if (btotal > 0)
{
for (c=4;c>=0;c--)
{
ctotal = btotal - c*50;
if (ctotal == 0)
{
// console.log(a+" 200s plus "+b+ " 100s plus "+c+" 50s is equal to 200");
count +=1;
}
if (ctotal > 0)
{
for (d=10;d>=0;d--)
{
dtotal = ctotal - d*20;
if (dtotal == 0)
{
// console.log(a+" 200s plus "+b+ " 100s plus "+c+" 50s plus " + d+" 20s is equal to 200");
count +=1;
}
if (dtotal > 0)
{
for (e=20;e>=0;e--)
{
etotal = dtotal - e*10;
if (etotal == 0)
{
// console.log(a+" 200s plus "+b+ " 100s plus "+c+" 50s plus " + d+" 20s plus "+ e + " 10s is equal to 200");
count +=1;
}
if (etotal > 0)
{
for (f=40;f>=0;f--)
{
ftotal = etotal - f*5;
if (ftotal == 0)
{
// console.log(a+" 200s plus "+b+ " 100s plus "+c+" 50s plus " + d+" 20s plus "+ e + " 10s plus " + f + " 5s is equal to 200");
count +=1;
}
if (ftotal > 0)
{
for (g=100;g>=0;g--)
{
gtotal = ftotal - g*2;
if (gtotal == 0)
{
// console.log(a+" 200s plus "+b+ " 100s plus "+c+" 50s plus " + d+" 20s plus "+ e + " 10s plus " + f + " 5s plus "+g+" 2s is equal to 200");
count +=1;
}
if (gtotal > 0)
{
for (h=200;h>=0;h--)
{
htotal = gtotal - h*1;
if (htotal == 0)
{
// console.log(a+" 200s plus "+b+ " 100s plus "+c+" 50s plus " + d+" 20s plus "+ e + " 10s plus " + f + " 5s plus "+g+" 2s plus " + h+ " 1s is equal to 200");
count +=1;
}
if (htotal > 0)
{
//
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
console.log(count);
@jakedobkin
Copy link
Author

I couldn't get the recursion to work- the closest I got was this- which gives a total of 11, instead of the 73K we need. But I don't understand recursion enough to figure out why that is!

#!/usr/local/bin/node

goal = 200;
count = 0; 
total = new Array();
lettervalue = [200,100,50,20,10,5,2,1];

function calculation(index)
    {
    for (var i=(200/lettervalue[index]);i>=0;i--)
        {
        total[index] = goal - i*lettervalue[index]; 
        if (total[index] == 0)
            {
            count +=1;
            }
        if (total[index] > 0)
            {
            goal = total[index];
            index = index+1;
            calculation(index);
            }
        }   
    }   


calculation(0);

console.log(count);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment