Skip to content

Instantly share code, notes, and snippets.

@stripedpurple
Created February 11, 2016 05:28
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 stripedpurple/9e1d5f1e1e2330c2ede7 to your computer and use it in GitHub Desktop.
Save stripedpurple/9e1d5f1e1e2330c2ede7 to your computer and use it in GitHub Desktop.
/*
Consider this puzzle
------------------------------------------------------------
- Start from the number 1
- Repeatedly, either adding 5 or multiplying by 3, an infinite amount of new numbers can be produced.
- How would you write a function that, given a number, tries to find a sequence of such additions and multiplications that produce that number?
- For example, the number 13 could be reached by first multiplying by 3 and then adding 5 twice
- Can number 15 be reached?
*/
function getPath(numAlpha) {
function getNode(present, past) {
if (present == numAlpha) {
return past;
}else if (present > numAlpha) {
return null;
}else{
return getNode(present + 5, "(" + past + " + 5)") || getNode(present * 3, "(" + past + " * 3)");
}
}
return getNode(1, "1");
}
console.log(getPath(24));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment