mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
minor refactor and comments
This commit is contained in:
@ -2,22 +2,21 @@
|
||||
|
||||
const num = 10001;
|
||||
|
||||
let primes = [2,3,5,7,11,13];
|
||||
let primes = [2,3,5,7,11,13]; // given list of primes you start with
|
||||
|
||||
const calculatePrime = (num) => {
|
||||
let count = primes.length;
|
||||
let current = primes[count-1] + 1;
|
||||
while (count < num) {
|
||||
let count = primes.length; // count number of primes calculated
|
||||
let current = primes[count-1] + 1; // current number being assessed if prime
|
||||
while (count < num) { // repeat while we haven't reached goal number of primes
|
||||
// go through each prime and see if divisible by the previous primes
|
||||
let prime = false;
|
||||
primes.some((n, i) => {
|
||||
if (current % n !== 0) {
|
||||
if (i === count-1) {
|
||||
prime = true;
|
||||
}
|
||||
} else {
|
||||
if (current % n === 0) {
|
||||
return true;
|
||||
}
|
||||
if (i === count-1) {
|
||||
prime = true;
|
||||
}
|
||||
})
|
||||
if (prime) {
|
||||
primes.push(current);
|
||||
|
Reference in New Issue
Block a user