I wrote a javascript program to return largest prime cofactor.My input is 600851475143.
But when i use my code in browser console than it takes very very long time.I want to increase my program speed and i tried many thing but it still it takes very large time.What should i do?
Here is my code:-
//object to store inforamtion realated to this program
//I have predeclared property instead of declaring them in loop to increase my program speed
var dataObject = {
a: [],
rem: 0,
x:0
};
//writing function to find cofacators of a number and store those in an array
function checkFactor(n){
var rem=0;
var a=[];
for(var i=1;i<=n;i++){
rem = n%i;
if(rem===0) a.push(i);
}
return a;
}
function operate(n){
var x; // this variable will be finally assigned largest prime cofactors
var subArray;
var mainArray = checkFactor(n); //array containing factors of n
if(mainArray.length===2) return mainArray[1];
for(var i=0;i<mainArray.length;i++){
subArray = checkFactor(mainArray[i]);
if(subArray.length===2) x=mainArray[i];
}
return x;
}