Skip to content

Instantly share code, notes, and snippets.

@ChickenFur
Created February 21, 2013 05:23
Show Gist options
  • Save ChickenFur/5002365 to your computer and use it in GitHub Desktop.
Save ChickenFur/5002365 to your computer and use it in GitHub Desktop.
magic number question
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