How do I get prime numbers 1-100 ?
Discussion posts and replies are publicly visible
----------------
gives all the prime numbers
a!localVariables( local!testWith:enumerate(tointeger(sqrt(ri!number)))+2, local!test:remove(local!testWith,local!testWith[length(local!testWith)-1]), where(a!forEach( items: local!test, expression:if(mod(ri!number,fv!item)=0,true,false()) ) ) )
------------------------------------------------------
P_getPrimenumbers
a!forEach( items: enumerate(100)+3, expression: if(length(rule!P_checkPrime(fv!item))>0 , {}, fv!item ) )
I would like to tweak the logic as the below code does not exclude 1 which is not a prime number since 1 has less than one factor, A prime number needs to have exactly two factors.
sum(mod(ri!number, enumerate(ri!number) + 1) = 0) <= 2
Do this instead!
length( wherecontains(true, mod(ri!number, enumerate(ri!number) + 1) = 0) ) = 2
OR
sum(mod(ri!number, enumerate(ri!number) + 1) = 0) = 2
Feel free to correct me if I am wrong.
a!localVariables( local!value: enumerate(ri!number) + 1, a!forEach( items: local!value, expression: if( length( wherecontains( 0, tointeger(mod(fv!item, local!value)) ) ) = 2, fv!item & " is Prime Number", {} ) ))
for optimizing, we don't have to check for all integers (1-n), we only need to check for 1 - sqrt(n)
sum(mod(ri!n, enumerate(sqrt(ri!n)) + 1) = 0) <= 1