How do I get prime numbers 1-100 ?
Discussion posts and replies are publicly visible
Hi Shilpa, Create an exp rule to check prime number and then use below code.
a!forEach( items: enumerate(100)+1, expression: if( rule!isPrimeNumber(fv!item), fv!item, {} ) )
filter( rule!isPrimeNumber(_), enumerate(100)+1, )
Clean..
can you please elaborate
rule!isPrimeNumber(_),
Hi Shilpa ,it can be an expression rule to find out whether a number is prime or not. You would need to create that rule. I would suggest you to create this logic .
Hi Shilpa,
Try this for prime number rule.
rule!isPrimeNumber(_) -->
load( local!count: a!forEach( items: enumerate(100) + 1, expression: if(mod(ri!number, fv!item) = 0, fv!item, {}) ), if(count(local!count) > 2, {}, ri!number))
The idea is to find the number of number by which the given number can be divided.
As the mod() function works with lists, I try the first 100 numbers. The compare operator "=" also works with lists. And the sum() interprets true and false like 1 and 0.
Less than three factors means it is a prime.
sum(mod(ri!number, enumerate(100) + 1) = 0) <= 2
"filter()", for those times when you'd rather have code that's nearly unreadable instead of 4 lines longer
I would only make one change which is to only enumerate up to the number being inspected.
sum(mod(ri!number, enumerate(ri!number) + 1) = 0) <= 2
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.
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.