Nodejs Number Prime Number Check primeFactors()

Here you can find the source of primeFactors()

Method Source Code

Number.prototype.primeFactors = function() {
  var number = this.valueOf();
  var primeFactors = [];

  isPrime = function(primeCandidate) {
    var p = 2;/*from ww w.  j  ava2  s.c  o  m*/
    var top = Math.floor(Math.sqrt(primeCandidate));
    while(p<=top){
      if(primeCandidate%p === 0){ return false; }
      p++;
    }
    return true;
  }

  findPrimeAfter = function(currentPrime) {
    nextPrimeCandidate = currentPrime + 1
    while(true) {
      if(isPrime(nextPrimeCandidate)){
        return nextPrimeCandidate;
      } else {
        nextPrimeCandidate++;
      }
    }
  }

  findPrimeFactors = function(x) {
    var primeFactorCandidate = 2;
    while(x != 1){
      while(x % primeFactorCandidate === 0 && x != 1 ){
        primeFactors.push(primeFactorCandidate);
        x /= primeFactorCandidate;
      }
      primeFactorCandidate = findPrimeAfter(primeFactorCandidate);
    }
  }

  findPrimeFactors(number);
  return primeFactors;
}

Related

  1. 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);
        for (let i = 0; i < primes.length; i++) {
            while (temp.isDevisor(primes[i])) {
                ans.push(primes[i]);
                temp = temp / primes[i];
        return ans;
    
  2. primeFactorization()
    Number.prototype.primeFactorization = function (){
        console.log("prime aangekomen");
        var ans = [];
        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;
    };
    
  3. primeFactorization()
    Number.prototype.primeFactorization = function () {
        "use strict";
        var ans = [];
        var primes = [];
        var temp = this;
        for (let i = 2; i <= this / 2; i += 1) {
            if (i.divisors().length == 2) {
                primes.push(i);
        for (let i = 0; i < primes.length; i += 1) {
            while (temp.isDivisor(primes[i])) {
                ans.push(primes[i]);
                temp = temp / primes[i];
        return ans;
    };
    
  4. primeFactors()
    "use strict";
    Number.prototype.primeFactors = function() {
      var v, i;
      v = null;
      for (i = 2; i < this - 1; i += 1) {
        if (this % i === 0) {
          v = i;
          break;
      if (v) {
        return [v].concat(Math.floor(this / v).primeFactors());
      } else {
        return [this];
    };
    console.log(Math.max.apply(null, (600851475143).primeFactors()));
    
  5. primeFactors()
    "use strict";
    var i, j, triangleNumber;
    Number.prototype.primeFactors = function() {
      var v, i;
      v = null;
      for (i = 2; i < this - 1; i += 1) {
        if (this % i === 0) {
          v = i;
          break;
    ...
    
  6. primeFactors()
    "use strict";
    var i, primeFactorLists;
    Number.prototype.primeFactors = function() {
      var v, i;
      v = null;
      for (i = 2; i < this - 1; i += 1) {
        if (this % i === 0) {
          v = i;
          break;
    ...
    
  7. sumPrimes(num)
    function sumPrimes(num) {
      var primes = [];
      for (var i = 2; i <= num; i++) {
        var notPrime = false;
        for (var current = 2; current <= (i - 1); current++) {
            if (i % current === 0) {
              notPrime = true;
        if (notPrime === false) 
          primes.push(i);
      var sum = primes.reduce(function(pv, cv) { 
                  return pv + cv;
                });
      return sum;
    sumPrimes(10);
    
  8. sumPrimes(num)
    function sumPrimes(num) {
      function isPrime(number) {
        for (var i = 2; i < number; i++) {
          if (number % i === 0) {
            return false;
        return true;
      var sum = 0;
      for (var j = 2; j <= num; j++) {
        if (isPrime(j)) {
          sum += j;
      return sum;
    sumPrimes(10);