Skip to content

Instantly share code, notes, and snippets.

@timrwood
Forked from 140bytes/LICENSE.txt
Created November 3, 2011 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timrwood/1337825 to your computer and use it in GitHub Desktop.
Save timrwood/1337825 to your computer and use it in GitHub Desktop.
Ways to make change for a dollar (or any other amount)

Ways to make change for a dollar - 140byt.es

The old mind puzzler, now as a 90 byte function.

var dollar = 100; var coins = [100, 50, 25, 10, 5, 1]; f(dollar, coins); // 293

var twoPound = 200; var coins = [200, 100, 50, 20, 10, 5, 2, 1]; f(twoPound, coins); // 73682

U mad Project Euler? http://projecteuler.net/problem=31

function f(
a, // target count
b, // array of currencies to use
c, // placeholder for current count
d, // placeholder for array index
e // placeholder for counter
) {
for (c |= e = 0; // initial: set counter(e) to zero. if current count(c) is undefined, set it to 0 to allow adding
b[d |= 0] ? // conditional: if we haven't hit the end of the index
c <= a : // evaluate whether we've gone over the limit
(e = c == a, 0); // else set the count to zero or 1 and return false to stop the loop
c += b[d] // increment: add currency to current count
)
e += f(a, b, c, d + 1); // recursion. use the next item in the array and call the same function, add it to the count
return e // return the count
}
function(a,b,c){var d=i=0,e=function(b){d+=b==a};while(c=b[i++])(function(b,c){e=function(d,e){for(e=d;e<=a;e+=b)c(e)}})(c,e);return e(0),d}
function f(a,b,c,d,e){for(c|=e=0;b[d|=0]?c<=a:(e=c==a,0);c+=b[d])e+=f(a,b,c,d+1);return e}
function f(a,b,c,d,e){for(c|=e=0;b[d|=0]?c<=a:(e=c==a,0);c+=b[d])e+=f(a,b,c,d+1);return e}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Tim Wood <timwoodcreates.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "currencyPermutations",
"description": "A currency permutation calculator (aka Project Euler 31)",
"keywords": [
"euler",
"currency",
"coins",
"permutation"
]
}
<!DOCTYPE html>
<title>Currency Permutations</title>
<div>Expected value: <b>293</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
function f(a,b,c,d,e){for(c|=e=0;b[d|=0]?c<=a:(e=c==a,0);c+=b[d])e+=f(a,b,c,d+1);return e}
document.getElementById( "ret" ).innerHTML = f(100, [100, 50, 25, 10, 5, 1]);
</script>
@tsaniel
Copy link

tsaniel commented Nov 4, 2011

It seems the test.html doesn't work?

@timrwood
Copy link
Author

timrwood commented Nov 4, 2011

Oops, extra parenthesis on the function call. It should work now.

@tsaniel
Copy link

tsaniel commented Nov 7, 2011

What about

function f(a,b,c,d,e){for(c|=e=0;b[d|=0]?c<=a:(e=c==a,0);c+=b[d])e+=f(a,b,c,d+1);return e}

?

@timrwood
Copy link
Author

timrwood commented Nov 7, 2011

Holy cow, shaved 50 bytes off? I didn't even think of using the function itself for the recursion.

@timrwood
Copy link
Author

timrwood commented Nov 7, 2011

I updated the annotated.js as best as I could, am I missing something?

@tsaniel
Copy link

tsaniel commented Nov 8, 2011

Hmm... I think you misunderstand the c |= e = 0.
It's actually c = c | (e = 0), which means each function call owns a counter, and |0 is to prevent the c as undefined in the first call.

@timrwood
Copy link
Author

timrwood commented Nov 8, 2011

That's what I meant, but now re-reading it, I can see how it's unclear. I meant 'use c as c if already set'. I'll clean it up.

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