How to write a simple algorithm to find Ramanujan numbers < N

120 viewsMathematicsProgramming
0

Here is a simple algorithm written in Javascript:-

<html>
 </html>
  <script>
 window.onload = init;
  function init() {
 var n = 1000000;
 for (var x = 1; x <= n; x++)
  findRamanujanNumber(x);
}
  function findRamanujanNumber(n) {
 var cube = 0, pivot = 0, count = 0;
 var obj_arr = [];
 for(var i = 1; i < n; i++) {
  cube = i*i*i;
  if (cube > n) {
   pivot = i;
   break;
  }
 }
  for(var j = 1; j < pivot; j++) {
  for (var k = j+1; k < pivot; k++) {
   rno = j*j*j + k*k*k;
   if (rno === n) {
    count++;
    var obj = {n : rno, a: j, b: k};
    obj_arr.push(obj);
    if (count === 2) {
     console.log("Ramanujan number is =", rno, "Pairs are: ", obj_arr);
     obj_arr = [];
    }
   }
  }
 }
}
 </script>

Answered question

Categories