Created
July 6, 2014 22:28
-
-
Save ryanseys/92be73e2021e05de88de to your computer and use it in GitHub Desktop.
Finding Ramanujan numbers
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
/** | |
* Find all possible sum of cubes for a particular number | |
* @param {Number} Number to find all sum of cube pairs | |
* @return {Array} Array of sum of cubes | |
*/ | |
function findAllPossiblePairs(n) { | |
var doneFinding = false; | |
var i = 1; | |
var found = []; | |
var j = Math.floor(Math.pow(n, 1/3)); | |
while(i <= j) { | |
var sum = Math.pow(i, 3) + Math.pow(j, 3); | |
if(sum === n) { | |
found.push([i, j]); | |
j--; | |
i++; | |
} | |
else if(sum > n) { | |
j--; | |
} | |
else { | |
i++; | |
} | |
} | |
return found; | |
} | |
/** | |
* A Ramanujan number has exactly two sum of cube pairs. | |
* @param {Number} x Number to check if it's a Ramanujan number | |
*/ | |
function isRamanujan(x) { | |
return findAllPossiblePairs(x).length === 2; | |
} | |
console.log(findAllPossiblePairs(1729), isRamanujan(1729)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment