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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:04:19+00:00 2026-05-29T10:04:19+00:00

I have an algorithm to test for primality, which uses the naive implementation as

  • 0

I have an algorithm to test for primality, which uses the naive implementation as listed here http://en.wikipedia.org/wiki/Primality_test#Naive_methods

       static boolean check(int n)
   {
           if(n == 2 || n == 3)
           {
                   return true;
           }
           if(n < 2 || n % 2 == 0 || n % 3 == 0)
           {
                   return false;
           }
           for(int i = 6; i * i <= n; i += 6)
           {
                   if(n % (i - 1) == 0 || n % (i + 1) == 0)
                   {
                           return false;
                   }
           }
           return true;
   }

I got all the way to the 6k+1 section, but after that, I’m lost. How else can I further optimize this for speed?

  • 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-29T10:04:20+00:00Added an answer on May 29, 2026 at 10:04 am

    If you want to stick with the naive method, then your next step is to use the next property listed in the wikipedia page you link to:

    So all prime numbers are of the form 30k + i for i = 1, 7, 11, 13, 17,
    19, 23, 29 (i.e. for i < 30 such that gcd(i,30) = 1).

    Except you might pick slightly different / more primes than 2.3.5

    You would replace the 6 stepping loop with a 30 stepping loop, (and check with all primes less than 30 by hand )

    The code might look like this:

        static boolean check(int n)
       {
               if(n<30)
               {
                  return n==2 || n==3 || n==5 || n==7 || ...
               }
    
               for(int i = 30; i * i <= n; i += 30)
               {
                  if (n % (i + 1))==0 return false;
                  if (n % (i + 7))==0 return false;
                  if (n % (i + 11))==0 return false;
                  if (n % (i + 13))==0 return false;
                  if (n % (i + 17))==0 return false;
                  if (n % (i + 19))==0 return false;
                  if (n % (i + 23))==0 return false;
                  if (n % (i + 29))==0 return false;
               }
               return true;
       }
    

    However you’ll note that this scans 8/30 (=27%) numbers, while the 6 stepping loop scans 2/6
    (=33%) So it scans about 20% less numbers, so you’d expect a speed up of at best 20%. As you add more primes to the list you get diminishing returns.

    Really if you need fast prime checking then you need to move away from the naive methods. And there’s been plenty of questions about those on stack overflow previously.

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

Sidebar

Related Questions

Below is Porters Stemming Algorithm for JavaScript which I have taken from here: http://tartarus.org/~martin/PorterStemmer/js.txt
I have a question regarding the std::sort algorithm. Here is my test code: struct
I have algorithm/computation in Java and unit test for it. The unit test expects
I have a recursive algorithm which steps through a string, character by character, and
I have a small algorithm which replaces the position of characters in a String:
I have created a test (not real) encryption function which takes a byte[] and
I have an image processing algorithm which makes of matrices, I have my own
I have a custom report which draws via Graphics2D , and uses a lot
I implemented the Miller-Rabin prime test algorithm found on wikipedia with Python 3. It
I need to implement and test an algorithm with a 2^n complexity. 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.