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 7642187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:07:34+00:00 2026-05-31T09:07:34+00:00

I need to print all the prime numbers less than a given number n.

  • 0

I need to print all the prime numbers less than a given number n. I can use sieve of Eratothenes but the running time of that algorithm IS NOT O(n). Is there any O(n) time solution for this problem?

  • 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-31T09:07:35+00:00Added an answer on May 31, 2026 at 9:07 am

    I don’t think you’ll find an algorithm for checking an arbitrary number for primality with an O(n) time complexity. I’m pretty certain the NSA (and any other organisations that deal with crypto issues) wouldn’t be very happy with that 🙂

    The only way you’ll get an O(n) or better way is to pre-calculate (for example) the first fifty million primes (or use someone else’s already-precalculated list) and use that as a lookup. I have such a file locally and it’s a simple matter of running grep over it to see if the number is prime. It doesn’t help for arbitrary numbers but I rarely have to use ones that big. Crypto guys, of course, would consider such a list vanishingly small for their purposes.

    And, if you turn it into a bitmap (about 120M for the first fifty million primes), you can even even reduce the complexity to O(1) by simply turning the number into a byte offset and bit mask – a couple of bit shifts and/or bitwise operations.

    However, getting a list of the primes below a certain n is certainly doable in O(n) time complexity. The Atkin and Bernstein paper detailing the Sieve of Atkin claims:

    We introduce an algorithm that computes the prime numbers up to N using O(N/log(log(N))) additions …

    which is actually slightly better than O(n).

    However, it’s still unlikely to compete with a lookup solution. My advice would be to use Atkin or Eratosthenes to make a list – it doesn’t really matter since you’ll only be doing this bit once so performance would not be critical.

    Then use the list itself for checking primality.

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

Sidebar

Related Questions

i need to print out numbers 1-100 in a random order. the print statement
I need to print the ASCII value of the given character in awk only.
I need to print last 20 characters of string, but only whole words. Delimiter
I need to find the greatest prime factor of a large number: up to
In Wikipedia this is one of the given algorithms to generate prime numbers: def
I'd like to build an efficient Python iterator/generator that yields: All composite numbers less
I need to print the date in the format of mm/dd/yyyy. if the date
I need to print records in a grid view, and need to preview it
I need to print a receipt from my web based apps using dot matrix
I need to print XML that I manipulated using groovy and maintain the attribute

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.