Vizible’s Blog

Python – Find Prime Number

Posted in Python by wearetherock on August 30, 2009
def get_prime_list(n):
        n  = int(n)
        maked_list = [False] * (n + 1)
        prime_list = []

        for i in range(2,n+1):
                if not maked_list[i] :
                        prime_list.append(i)
                        for j in range(i**2, n+1,i):
                                maked_list[j] = True
        return prime_list
print get_prime_list(10001)
~
Advertisements

One Response

Subscribe to comments with RSS.

  1. wearetherock said, on August 31, 2009 at 2:43 pm
    from math import sqrt
    
    primes = [2]
    
    next_number = 2
    while len(primes) < 10001:
    
            next_number += 1
            print len(primes)
            if sqrt(next_number) in primes :
    
                    print 'not '
                    continue
            for prime in primes :
    
                    if next_number % prime == 0:
                            break;
    
                    count = 1
                    lists = [x for x in range(2,next_number) if not x in primes]
    
                    for i in range(2,next_number):
                            if next_number % i == 0 :
    
                                    count += 1
                    if not count > 1 :
    
                            primes.append(next_number)
                            break
    print primes
    print 'len ',len(primes)
    
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: