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

  • Home
  • SEARCH
  • 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 8313189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:24:30+00:00 2026-06-08T20:24:30+00:00

After 10 minutes of work I have written a function presented below. It returns

  • 0

After 10 minutes of work I have written a function presented below. It returns a list of all primes lower than an argument. I have used all known for me programing and mathematical tricks in order to make this function as fast as possible. To find all the primes lower than a million it takes about 2 seconds.

Do you see any possibilities to optimize it even further? Any ideas?

def Primes(To):
  if To<2:
    return []
  if To<3:
    return [2]
  Found=[2]
  n=3
  LastSqr=0
  while n<=To:
     k=0
     Limit=len(Found)
     IsPrime=True
     while k<Limit:
         if k>=LastSqr: 
            if Found[k]>pow(n,0.5): 
               LastSqr=k
               break
         if n%Found[k]==0:
            IsPrime=False
            break
         k+=1
     if IsPrime:
        Found.append(n)
     n+=1
  return Found
  • 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-06-08T20:24:32+00:00Added an answer on June 8, 2026 at 8:24 pm

    You can use a couple tricks to speed things up, using the basic sieve of erastothenes. One is to use Wheel Factorization to skip calculating numbers that are known not to be prime. For example, besides 2 and 3, all primes are congruent to 1 or 5 mod 6. This means you don’t have to process 4 of every 6 numbers at all.

    At the next level, all primes are congruent to 1, 7, 11, 13, 17, 19, 23, or 29, mod 30. You can throw out 22 of every 30 numbers.

    Here is a simple implementation of the sieve of Erastothenes that doesn’t calculate or store even numbers:

    def basic_gen_primes(n):
        """Return a list of all primes less then or equal to n"""
        if n < 2:
            return []
    
        # The sieve.  Each entry i represents (2i + 1)
        size = (n + 1) // 2
        sieve = [True] * size
    
        # 2(0) + 1 == 1 is not prime
        sieve[0] = False
    
        for i, value in enumerate(sieve):
            if not value:
                continue
    
            p = 2*i + 1
    
            # p is prime.  Remove all of its multiples from the sieve
            # p^2 == (2i + 1)(2i + 1) == (4i^2 + 4i + 1) == 2(2i^2 + 2i) + 1
            multiple = 2 * i * i + 2 * i 
            if multiple >= size:
                break
    
            while multiple < size:
                sieve[multiple] = False
                multiple += p 
    
        return [2] + [2*i+1 for i, value in enumerate(sieve) if value]
    

    As mentioned, you can use more exotic sieves as well.

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

Sidebar

Related Questions

I have a pop up which is timed to appear after 2 minutes on
I'm making a php file that will run an event after five minutes have
I have solution with few projects for calculation proposes. Code after 20 minutes of
sometimes after some minutes i have to refresh browser to get my web application
My app has a session timeout after 30 minutes. If the user has a
Possible Duplicate: How do I expire a PHP session after 30 minutes? How to
I need to trigger a block of code after 20 minutes from the AlarmManager
iam trying play animation with xcode,in specefic Time for example after 3 minutes play
My site has a timeout set, so after 15 minutes of in-activity a login
I developed a program which after every two minutes call db to check new

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.