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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:04:22+00:00 2026-05-25T03:04:22+00:00

Assuming you were going to write a function / method to find a prime

  • 0

Assuming you were going to write a function / method to find a prime number, what would be the most efficient way to do this? I’m thinking that it would be a test that is something like this:

Code Below in semi-c++

bool primeTest (int x) { //X is the number we're testing
    int testUpTo = (int)((sqrt(x))+1);
    for (int i=3; i<testUpTo; i+=2){
        if ((x%i)==0) {
            return false;
        }
    }
    return true;
}

Does someone have a better way to go about solving this that will take less computations?

edit: Changed code slightly, twice. I didn’t write this with any specific language in mind, although I suppose it’s C++ over java due to the word bool.

  • 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-25T03:04:22+00:00Added an answer on May 25, 2026 at 3:04 am

    I would use the Miller Rabin test, which can easily be made deterministic for numbers smaller than 341,550,071,728,321 (and 2^31 is much smaller than that).

    Pseudocode: there are a number of different cases.

    1. x smaller than 9: Return (x & 1) != 0 || x == 2
    2. x smaller than about 200 (tweakable): use trial division (what you used)
    3. x smaller than 1373653: use Miller Rabin with bases 2 and 3.
    4. x smaller than 4759123141 (that is everything else): use Miller Rabin with bases 2, 7 and 61.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming such a query exists, I would greatly appreciate the help. I'm trying to
Assuming network access is sporadic with no central server, what would be the best
Assuming a fluid layout is not an option (since that is a different discussion
This function is called to serve each client in a new thread. in the
This question is going to be rather long, so I apologize preemptively. In Python
Given certain multibyte character sets, am I correct in assuming that the following doesn't
Surprisingly I've been unable to find anyone else really doing this, but surely someone
Assuming I had a collection like this.. var list = new List<Item> { new
Problem: I want to write a process that will allow a user to take
It's hard to ask search this question and the answer would help clean up

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.