Created
February 21, 2013 05:23
-
-
Save ChickenFur/5002365 to your computer and use it in GitHub Desktop.
magic number question
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
var infinteNumber = [4, 0, 20, 42,]; | |
var magicNumberList = {}; | |
var isMagic = function(num){ | |
if(magicNumberList[num]) | |
return true; | |
var stringNum = num.toString(); | |
var arrayOfDigits = stringNum.split("") | |
console.log (num) | |
var magicNumberTotal = 0; | |
for(var i = 0; i < stringNum.length; i++){ | |
magicNumberTotal += (arrayOfDigits[i] * arrayOfDigits[i]); | |
} | |
if(magicNumberTotal === 1){ | |
magicNumberList[num] = true; | |
return true; | |
}else if (infinteNumber.indexOf(magicNumberTotal) >= 0){ | |
return false; | |
} | |
else{ | |
return magicNumberTotal; | |
} | |
} | |
var checkRecursive = function (num){ | |
result = isMagic(num); | |
if ( result === true){ | |
return true; | |
} | |
if (result === false){ | |
return false; | |
} | |
else{ | |
return checkRecursive(result); | |
} | |
} | |
var magicNumFromZeroTo = function(n){ | |
results = []; | |
for( var i = 0; i <= n; i ++){ | |
if( checkRecursive(i) ){ | |
results.push(i); | |
} | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment