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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:42:04+00:00 2026-06-14T09:42:04+00:00

I have got one question to print one million prime numbers . I have

  • 0

I have got one question to print one million prime numbers . I have written a java program for that .. It’s currently taking 1.5 mins approx to calculate it .. I think my solution is not that efficient. I have used the below algo:

  • Adding 1 2 3 to the prime list initially
  • Calculating the last digit of the number to be checked
  • Checking if the digit is 0 , 2 or 4 or 6 or 8 then skipping the number
  • else calculating the square root of the number ..
  • Trying to Divide the number starting from 2 till the square root of the number
  • if number is divisible then skipping the number else adding it to the prime list

I have read several other solutions as well , but I didn’t find a good answer. Please suggest ideally what should be approx minimum time to calculate this and what changes are required to make the algorithm more efficient.

  • 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-14T09:42:06+00:00Added an answer on June 14, 2026 at 9:42 am

    A simple sieve of Eratosthenes runs like the clappers. This calculates the 1,000,000th prime in less than a second on my box:

    class PrimeSieve
    {
        public List<int> Primes;
    
        private BitArray Sieve;
    
        public PrimeSieve(int max)
        {
            Primes = new List<int> { 2, 3 }; // Must include at least 2, 3.
            Sieve = new BitArray(max + 1);
            foreach (var p in Primes)
                for (var i = p * p; i < Sieve.Length; i += p) Sieve[i] = true;
        }
    
        public int Extend()
        {
            var p = Primes.Last() + 2; // Skip the even numbers.
            while (Sieve[p]) p += 2;
            for (var i = p * p; i < Sieve.Length; i += p) Sieve[i] = true;
            Primes.Add(p);
            return p;
        }
    }
    

    EDIT: sieving optimally starts from p^2, not 2p, as Will Ness correctly points out (all compound numbers below p^2 will have been marked in earlier iterations).

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

Sidebar

Related Questions

Basically, I have the same question as this one , which unfortunately never got
i have done some learning on struts based on one project that i got.Now
I have got one weird issue. I am working on an asp .net mvc
I have got as follows: use constant ABC => ('one', 'two', 'three'); and I
In Unix, I have got three main files. One of them is a library
I have run an ssis package and got error in one of the tasks(the
This one's got me stumped and it's driving me nuts. I have a SiteFinity
I got a simple problem in SQLAlchemy. I have one model in a table,
I've got two Activities. In one I calculate some things and therefore I have
I've got an asp.net gridview inside an updatepanel. One of the fields I have

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.