โกวิโท

จำนวนเฉพาะกับตะแกรงเอราทอสเทนีส

by Kowit on ก.ค..30, 2009, under Programming, Python

ไปอ่านเจอมา เขียนไว้กันลืม

def primes(n):
"""
primes(n) --> primes

Return list of primes from 2 up to but not including n.
Uses Sieve of Erasth.
"""

    if n < 2:
        return []

    nums = range(2,int(n))
    p = []

    while nums:
        new_prime = nums[0]
        p.append(new_prime)

        for i in nums[1:]:
            if i % new_prime == 0:
                nums.remove(i)
        nums.remove(nums[0])

    return p

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!