Skip to content

Instantly share code, notes, and snippets.

@drzhbe
Last active December 22, 2015 13:59
Show Gist options
  • Save drzhbe/6483104 to your computer and use it in GitHub Desktop.
Save drzhbe/6483104 to your computer and use it in GitHub Desktop.
primes
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