Created
February 11, 2016 05:28
-
-
Save stripedpurple/9e1d5f1e1e2330c2ede7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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