Naturally, for bool isprime(number)
there would be a data structure I could query.
I define the best algorithm, to be the algorithm that pr
I have got a prime function which works until (2^61)-1 Here:
from math import sqrt
def isprime(num): num > 1 and return all(num % x for x in range(2, int(sqrt(num)+1)))
Explanation:
The all()
function can be redefined to this:
def all(variables):
for element in variables:
if not element: return False
return True
The all()
function just goes through a series of bools / numbers and returns False
if it sees 0 or False
.
The sqrt()
function is just doing the square root of a number.
For example:
>>> from math import sqrt
>>> sqrt(9)
>>> 3
>>> sqrt(100)
>>> 10
The num % x
part returns the remainder of num / x.
Finally, range(2, int(sqrt(num)))
means that it will create a list that starts at 2 and ends at int(sqrt(num)+1)
For more information about range, have a look at this website!
The num > 1
part is just checking if the variable num
is larger than 1, becuase 1 and 0 are not considered prime numbers.
I hope this helped :)