Tuesday, February 16, 2016

first 100 prime numbers


Using sieve of Eratosthenes method to find first 100 prime numbers..

The program might not be exact ..its learning curve will improve on.. :)


#!/usr/bin/env python
"""
  Program to list out the first 100 prime numbers
  sieve of Eratosthenes Method
"""


def getprime(n):
    ret=[]
    cancel=[]
    for i in range( 2, n+1):
        if i not in cancel:
            ret.append(i)
        for j in range (i,n+1,i):
            cancel.append(j)
    return ret


if __name__=='__main__':

print getprime(100)

Take a look: 
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
https://en.wikipedia.org/wiki/Prime_number

No comments:

Post a Comment