Forum Discussion
Lambda for Prime Numbers in a Range
I had a run through for ideas. I got to BYROW/MOD/AND but found you were there ahead of me.
You halved the count of numbers to test by eliminating even numbers. A further reduction could be to remove multiples of 3 as well by generating numbers
= LAMBDA(low,high,
LET(
k, SEQUENCE(QUOTIENT(high-low,3),1,1+MROUND(low,6),3),
k+ISEVEN(k)
)
)
Another thought is to perform the extraction in tranches. For example, to identify primes in the range 32-1000 you only need to test against the 10 primes in the range 2-31. That is not too exciting but, for the next tranche 1,000-1,000,000, you only need to test against the ~144 primes over 2-997 that you have already found.
As for timing, I use Charles Williams's functions that time calculations to μs.
I carried out the 3-step process and took 5sec to determine 78330 supposed primes between a thousand and a million. My active range wen to FR333002 which is probably a cell I have never seen before. It was probable a mistake to lay the test primes out as a row array. Both could have been columns if I were to use MAP rather than BYROW for the modulo calculation.
= FILTER( number#, MAP(number#, LAMBDA(n, AND(MOD(n,prime)))))
Note: This did improve the used range,