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
My solution:
Start with a few simple primes, such as local!primes:{2,3,5,7,13}
forEach( items: enumerate(100) +1
expression: if
any(local!candidate = fv!item, /*any() function returns true if any of the elements in a boolean array are true*/
forEach( items: local!primes)
expression: mod(fv!candidate, fv!item) = 0 /*if a prime evenly divides your candidate number, it's not prime)
true, /*true in this case means it's true that it's not prime*/
false
), /*the for loop inside the any() function is just the first parameter of the if, if any are true, then it's not prime*/
null, /*so if it's not prime, don't return it, or if none of the primes we picked out divide it, maybe it's prime, return it*/
fv!item
)
Take the output of this and append it to local!primes to refine it. You can keep adding more and more to get more and more primes. It starts to get time consuming up into the 4 and 5 digit primes, but it will continue to work if you continue to add to the starting primes.
You could also just go to wolfram alpha and copy and paste the answer into a constant.
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