Sunday, October 24, 2010

Generating prime numbers upto n-th number.

#primenumbers.py
num= #insert a number here       e.g num=1000
pp=2
ps=[pp] #here, pp is stored into the list, ps.
pp+=1
ps.append(pp)

while pp<int(num):
    pp+=2
    test=True
    for a in ps:
        if pp%a==0: test=False
    if test: ps.append(pp)
 
print (ps)
This program will generate all prime numbers upto the given number, num. To increase the efficiency of the operation, it only checks odd numbers for whether they are prime(since, 2 is the only even prime number).

If you have better methods of completing this program, do share them here!!