fork(59) download
  1. //Counts the number of primes under 25
  2. function primesUnder25(){
  3. // Candidates will start out by holding the numbers from 1 to 25
  4. var candidates = [];
  5. var i;
  6. do{
  7. // Add to the list and then increment
  8. candidates += i;
  9. i++;
  10. } while(i < 25) // Stop once we get to 25
  11.  
  12. // We have to remove all compostes, and remove 1.
  13. // Every composite under 25 is divisible by 2 or 3
  14. // First we will generate and remove all multiples of 2 and 3
  15. var composites = []
  16. for(var prime of [2,3]){
  17. for(var i=1; (prime ** i) <= 25; i++){ // Multiply each prime by each i to generate all multiples of that prime
  18. composites.push(prime ** i)
  19. }
  20. }
  21. // For each composite number, we will remove the candidate at that index
  22. composites = composites.sort().reverse(); // (Do this from biggest to smallest or else the array indices shift after you delete each one)
  23. composites.forEach(ind => {
  24. candidates=candidates.slice(0, ind)+candidates.slice(ind+1) // Remove at (ind)th index
  25. })
  26.  
  27. // Lastly, don't forget to remove 1 because that's not prime
  28. candidates = candidates.slice(1)
  29. return candidates;
  30. }
  31.  
  32. console.log(primesUnder25()) // Huh, that's weird... seems to work though, so good enough for me.
Success #stdin #stdout 0.02s 17388KB
stdin
Standard input is empty
stdout
nine