Skip to content

Instantly share code, notes, and snippets.

@ryanseys
Created July 6, 2014 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanseys/92be73e2021e05de88de to your computer and use it in GitHub Desktop.
Save ryanseys/92be73e2021e05de88de to your computer and use it in GitHub Desktop.
Finding Ramanujan numbers
/**
* 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