The Python function
def isPrime(n): return all(n % i for i in xrange(2, n))
checks if a number is a prime number by using
How can I write a function similar to
all in Mathematica?
initial side note: As J.M. correctly points out this is not an efficient implementation and serves only to illustrate behavior similar to the Python function
If you are looking for a similar definition to the Python code you give, then you could use this:
This creates a table of either
You can then find primes under 20 using:
This however includes 1, which it should not. You could also just use the build in prime checker:
You could pass around unevaluated iteration specfications quite simply however, allowing you to iterate in any way you see fit in functions that take these specifications as input. For instance implementing an
Allowing the prime function you gave to be defined as:
Which still includes 1 however.
|show 3 more comments|