Last active
December 22, 2015 13:59
-
-
Save drzhbe/6483104 to your computer and use it in GitHub Desktop.
primes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function prime() { | |
this.primes = [], | |
this.number = 1, | |
this.stop = false, | |
this.x = 600851475143, | |
this.checkPrime = function() { | |
var number = this.number, | |
i = 0, | |
l = this.primes.length; | |
if (!l) return true; // первое число - простое | |
for ( ; i < l; i++) { | |
if (number % this.primes[i] == 0) return; | |
} | |
return true; // если в цикле число ни разу не поделилось без остатка - оно простое | |
}, | |
this.divideXbyAllPrimes = function() { | |
for (var i = 0, l = this.primes.length; i < l; i++) { | |
this.divideX( this.primes[i] ); | |
} | |
}, | |
this.divideX = function(prime) { | |
while(this.x % prime == 0) { | |
this.x /= prime; | |
console.log('YO: ', this.x); | |
} | |
}, | |
this.solve = function() { | |
this.stop = false; | |
while(this.x != 1) { | |
this.number++; | |
console.log(this.number); | |
if (this.checkPrime()) { | |
this.primes.push(this.number); | |
this.divideXbyAllPrimes(); | |
} | |
} | |
} | |
} | |
var P = new prime(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment