Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6006197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:30:18+00:00 2026-05-23T01:30:18+00:00

def primes(n): if n==2: return [2] elif n<2: return [] s=range(3,n+1,2) mroot = n

  • 0
def primes(n): 
    if n==2: return [2]
    elif n<2: return []
    s=range(3,n+1,2)
    mroot = n ** 0.5
    half=(n+1)/2-1
    i=0
    m=3
    while m <= mroot:
        if s[i]:
            j=(m*m-3)/2
            s[j]=0
            while j<half:
                s[j]=0
                j+=m
        i=i+1
        m=2*i+3
    return [2]+[x for x in s if x]

Hello, I found this function here http://code.activestate.com/recipes/366178-a-fast-prime-number-list-generator/ and I’m stuck. I don’t understand this. I think it’s using some properties of prime numbers but all those single letter variables are so mysterious. Could you please shed some light?

This I understand:
mroot is the limit of the numbers you want to check for primality
and I know that the function changes the list s to 0’s marking the multiples. I also understand the list comprehesion at the end, and I understand s.

But why half? What is j? What is m?

Could you please give some comment?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:30:19+00:00Added an answer on May 23, 2026 at 1:30 am

    A line-by-line breakdown:

    def primes(n): 
        if n==2: return [2]
    

    This function returns a list of primes <= n. So if n == 2, that list contains only 2. Easy enough.

        elif n<2: return []
    

    Here again, there are no prime numbers below 2, so return an empty list.

        s=range(3,n+1,2)
    

    So s is a list of odd numbers starting with 3 and going to n + 1. s stands for sieve — this is a sieve algorithm, which means that composite (non-prime) numbers will be sifted out of the list. In effect, they will be “crossed out.” See below for a detailed description of sieve algorithms.

        mroot = n ** 0.5
    

    Since it’s a sieve, we can stop the algorithm once we’ve hit the square root of n.

        half=(n+1)/2-1
    

    This is an explicit formula for the length of s; it could be replaced with len(s), but that might take longer to calculate for large values of n. This will also be useful for terminating certain parts of the algorithm.

        i=0
        m=3
    

    I is our index; i simply steps through the sieve, checking each value. If the value is 0, then the number has been “crossed off” because it isn’t prime. m is simply the value of s[i] at any given moment; a later line keeps it updated.

        while m <= mroot:
            if s[i]:
    

    Since s[i] evaluates to True, it hasn’t been crossed off the list yet. That means it’s prime! So now we have to figure out which numbers on the list are multiples of s[i] — they are all non-primes, and should be crossed off the list.

                j=(m*m-3)/2
                s[j]=0
    

    Now the fun starts. Because the sieve isn’t a list of consecutive numbers, but a list of odd numbers, we have to figure out where the multiples of our prime live in s. In this case, our prime is 3, so we need to find the index of 9, 15, 21, 27… (we don’t have to find 6, 12, 18… because they’re even, and so not in the list). This particular technique for finding the indices is really clever, because the author has figured out that once all the multiples of a particular prime have been crossed out, they can be skipped. That means the first un-crossed-out multiple of our prime is actually the square of that prime. (So for example, if the prime were 7, 7 * 3 = 21 and 7 * 5 = 35 would already have been crossed out, so the first multiple of 7 that we have to deal with is 7 * 7.) Once that makes sense, it’s pretty easy to see that the location of 9 in s is (9 – 3) // 2 (where // is floor division).

                while j<half:
                    s[j]=0
                    j+=m
    

    Now it goes back to being easy. We’ve found 9; now we have to find 15 = 9 + 3 + 3. Since s contains only odd numbers, it’s half as long as a list with every number; to skip ahead 6 then, we need only add 3 to j. We do this as long as j is less than half — in other words, as long as j is less than the length of s.

            i=i+1
            m=2*i+3
    

    Again, easy — i is just the index of the list, while m is the value of the number that was originally there. (You can test it out to see why: [2 * i + 3 for i in range(10)].)

        return [2]+[x for x in s if x]
    

    And voila — filter the zeros out of the sieve, prepend [2] and you have a list of primes.

    The most confusing thing about this algorithm has to do with the shortcuts the author has taken, which make this run faster, but fog up the fundamental concept. (In fact, there are even more shortcuts one could take, but that’s another post.) Here’s a much simpler sieve that shows the basic idea:

    >>> numbers = range(40)
    >>> numbers[1] = 0    # 1 isn't prime
    >>> for i in numbers:
    ...     if i:
    ...         for j in range(i + i, len(numbers), i):
    ...             numbers[j] = 0
    ... 
    >>> [n for n in numbers if n]
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    

    To spell it all out, first numbers looks like this:

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...]
    

    Then…

    [0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10...]
    [0, 0, 2, 3, 0, 5, 0, 7, 0, 9, 0...]
    [0, 0, 2, 3, 0, 5, 0, 7, 0, 0, 0...]
    

    And so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Look at this simple function def prime_factors(n): for i in range(2,n): if n %
This is code an algorithm I found for Sieve of Eratosthenes for python3. What
This is the best algorithm I could come up. def get_primes(n): numbers = set(range(n,
I wrote this simple code in python to calculate a given number of primes.
Is the following code for generating primes pythonic? def get_primes(n): primes=[False,False]+[True]*(n-1) next_p=(i for i,j
def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj =
def record return unless @supported klasses = profile_options[:formats].map { |f| RubyProf.const_get(#{f.to_s.camelize}Printer) }.compact klasses.each do
def myFunc(arg1, arg2): print This is a test with + arg1 + and +
How do I do mutually recursive definitions in Clojure? Here is a code in
def merge(l1,l2): i=0; while((l1[i]!=none)||(l2[i]!=none)): SyntaxError: invalid syntax being a newbie i can't figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.