//Counts the number of primes under 25
function primesUnder25(){
// Candidates will start out by holding the numbers from 1 to 25
var candidates = [];
var i;
do{
// Add to the list and then increment
candidates += i;
i++;
} while(i < 25) // Stop once we get to 25
// We have to remove all compostes, and remove 1.
// Every composite under 25 is divisible by 2 or 3
// First we will generate and remove all multiples of 2 and 3
var composites = []
for(var prime of [2,3]){
for(var i=1; (prime ** i) <= 25; i++){ // Multiply each prime by each i to generate all multiples of that prime
composites.push(prime ** i)
}
}
// For each composite number, we will remove the candidate at that index
composites = composites.sort().reverse(); // (Do this from biggest to smallest or else the array indices shift after you delete each one)
composites.forEach(ind => {
candidates=candidates.slice(0, ind)+candidates.slice(ind+1) // Remove at (ind)th index
})
// Lastly, don't forget to remove 1 because that's not prime
candidates = candidates.slice(1)
return candidates;
}
console.log(primesUnder25()) // Huh, that's weird... seems to work though, so good enough for me.
Ly9Db3VudHMgdGhlIG51bWJlciBvZiBwcmltZXMgdW5kZXIgMjUKZnVuY3Rpb24gcHJpbWVzVW5kZXIyNSgpewoJLy8gQ2FuZGlkYXRlcyB3aWxsIHN0YXJ0IG91dCBieSBob2xkaW5nIHRoZSBudW1iZXJzIGZyb20gMSB0byAyNQoJdmFyIGNhbmRpZGF0ZXMgPSBbXTsKICAgIHZhciBpOwoJZG97CgkJLy8gQWRkIHRvIHRoZSBsaXN0IGFuZCB0aGVuIGluY3JlbWVudAoJCWNhbmRpZGF0ZXMgKz0gaTsKCQlpKys7Cgl9IHdoaWxlKGkgPCAyNSkgLy8gU3RvcCBvbmNlIHdlIGdldCB0byAyNQoJCiAgICAvLyBXZSBoYXZlIHRvIHJlbW92ZSBhbGwgY29tcG9zdGVzLCBhbmQgcmVtb3ZlIDEuCgkvLyBFdmVyeSBjb21wb3NpdGUgdW5kZXIgMjUgaXMgZGl2aXNpYmxlIGJ5IDIgb3IgMwoJLy8gRmlyc3Qgd2Ugd2lsbCBnZW5lcmF0ZSBhbmQgcmVtb3ZlIGFsbCBtdWx0aXBsZXMgb2YgMiBhb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