let classicCalculation = (n) => {
let a = [];
for(let i = 1; i <= n; i++){
if( n % i == 0 ){
a.push(i);
}//if
}//for
return a.length ? a : `${n} is prime`;
}//function
// performance analysis
let t0 = performance.now();
classicCalculation(5245396);
let t1 = performance.now();
console.log("Call to classicCalculation took " + (t1 - t0) + " milliseconds."); //took 39.700000001175795 milliseconds.
// Better way to implement this
betterCalculatino= (n) =>{
let v = []
let a = []
for(let i = 2; i<= Math.sqrt(n); i++){
if ( n % i == 0){
if ( n / i == i){
v.push(i)
} else {
v.push(i)
a.push(n/i)
}
}
}
for(let p = a.length-1; p >= 0; p--){
v.push(a[p]);
}
return v.length ? v : `${n} is prime number`;
}
let t2= performance.now();
betterCalculatino(5245396);
let t3 = performance.now();
console.log("Call to better took " + (t3 - t2) + " milliseconds."); // 1.7000000007101335 milliseconds