Discussion Room : IDC101

Number of primes less than or equal to a given number, for a range of values

Re: Number of primes less than or equal to a given number, for a range of values

by Aabhas Gulati . -
Number of replies: 0

Sir , me and Mayank Yadav (MS18062) implemented your suggestions and came up with a code which uses the sieve .


THE FORMAT FOR THE CODE . 

n=10000

l=range(3,n,2)

primes=[2]

i=0

while i**2<n:

    q=l[i]

    if q!=0:

        primes.append(q)

        for x in range(0,n/(2*q)):

            l[i+x*(q)]=0

    i+=1

for x in range(1,(n-1)/2):

    if l[x]!=0:

        primes.append(l[x])

del primes[-1]

print len(primes)



This program terminates in 0.003 for n=10000 . 

THANK YOU .