Created
August 6, 2015 12:28
-
-
Save larryprice/017fa2490f515abb486a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| if (process.argv.length !== 4) { | |
| console.log("Usage:", process.argv[1], "b start") | |
| return; | |
| } | |
| var b = parseInt(process.argv[2]), | |
| start = parseInt(process.argv[3]); | |
| if (isNaN(b) || isNaN(start)) { | |
| console.log("need numbers"); | |
| return; | |
| } | |
| var mathIt = function (exp, num) { | |
| var ind = (num + "").split(""), | |
| total = 0; | |
| for (var i = 0; i < ind.length; ++i) { | |
| total += Math.pow(ind[i], exp); | |
| } | |
| return total; | |
| }; | |
| var findPattern = function (numbers) { | |
| if (numbers.length <= 1) { | |
| return; | |
| } | |
| var newNumber = numbers.slice(-1)[0]; | |
| var idx = numbers.slice(0, -1).indexOf(newNumber) | |
| if (idx !== -1) { | |
| return numbers.slice(idx, -1); | |
| } | |
| return null; | |
| } | |
| var sadCycle = function (exp, num) { | |
| var results = [], | |
| pattern = null; | |
| while (!pattern) { | |
| num = mathIt(exp, num); | |
| results.push(num); | |
| pattern = findPattern(results); | |
| } | |
| return pattern; | |
| } | |
| console.log(sadCycle(b, start)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment