Today I bought a new book about programming and algorithms. One of the first examples in the book, which I found very simplistic yet interesting, is the algorithm for finding prime numbers. It goes like this:
- Let N be the number you want to find if it is prime or not
- Let X be 2
- If N equals X, N is a prime. End.
- Divide N by X
- If the result is an integer, N is not prime. End.
- Add 1 to X.
- Go to step 3.
def isitprime(n):
x=2
while True:
if n==x:
return True
elif n%x == 0:
return False
x+=1