In a given array of input x can you find all the prime numbers smaller than x? This can be done with a for loop and I provided my code below. However, I'm really interested in knowing if this can be done without a for loop. Was thinking of a possible way using the filter method?
My for loop code
function findThePrimes(num) {
let nonPrimes = [], i, j, primes = [];
for (i = 2; i <= num; ++i) {
if (!nonPrimes [i]) {
primes.push(i);
for (j = i << 1; j <= num; j += i) {
nonPrimes[j] = true;
}
}
}
return primes;
}
console.log(findThePrimes(100))
Looking for something similar to the code below
function findThePrimes(num) {
numArr = Array.from({length: num}, (v, k) => k+1)
const primeNum = []
const takeOutPrimes = numArr.filter(num => ...not sure what to do next that will push prime numbers into the primeNum array)
}
console.log(findThePrimes(ANY_NUMBER))