Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Created December 18, 2011 05:19
Show Gist options
  • Save ishiduca/1492449 to your computer and use it in GitHub Desktop.
Save ishiduca/1492449 to your computer and use it in GitHub Desktop.
任意の順番で「+5」か「*3」して、任意の値にする過程を示す 初期値は「1」
function findSequence (goal) {
var find = function (start, history) {
return (start === goal) ? history :
(start > goal) ? null :
find(start + 5, "(" + history + " + 5)") ||
find(start * 3, "(" + history + " * 3)");
};
return find(1, '1');
}
console.log(findSequence(24));
// (((1 * 3) + 5) * 3)
console.log(findSequence(68));
// ((((((1 + 5) + 5) + 5) + 5) * 3) + 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment