This Blog is Dead, follow me at my new blog

You can now follow my adventures at my new blog, http://julianapena.com/

Wednesday, July 4, 2007

Programming: Find prime numbers

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:

  1. Let N be the number you want to find if it is prime or not
  2. Let X be 2
  3. If N equals X, N is a prime. End.
  4. Divide N by X
  5. If the result is an integer, N is not prime. End.
  6. Add 1 to X.
  7. Go to step 3.
In Python, this can be implemented with the following function:
def isitprime(n):
x=2

while True:
if n==x:
return True
elif n%x == 0:
return False
x+=1