Javascript Number primeFactorization()

Description

Javascript Number primeFactorization()

Number.prototype.primeFactorization = function(){
    var ans = [];
    var primes = [];
    var temp = this;
    for(let i = 2; i<=temp/2;i++){
        if(i.divisors().length == 2){
            primes.push(i);//from  w w w . ja va  2 s.co m
        }
    }
    
    for(let i =0; i<primes.length; i++){
        while(temp.isDivisor(primes[i])){
            ans.push(primes[i]);
            temp = temp / primes[i];
        }
    }
    return ans;
}

Javascript Number primeFactorization()

Number.prototype.primeFactorization = function() {
  var ans = [];//from   ww w  .  j ava  2  s  .co  m
  var primes = [];
  var temp = this;
  for (let i = 2; i < temp/2; i++){
    if(i.divisors().length == 2){
      primes.push(i);
    }
  }
  for (let i = 0; i < primes.length; i++){
    while (temp.isDivisor(primes[i])){
      ans.push(primes[i]);
      temp = temp / primes[i];
    }
  }
  return ans;
}



PreviousNext

Related