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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:11:01+00:00 2026-05-18T08:11:01+00:00

A naive algorithm for computing prime numbers exists. For example, you could use a

  • 0

A naive algorithm for computing prime
numbers exists. For example, you could
use a while loop to check that c % i
!= 0 for all positive integer i such
that i > 1 and i < c.

However, it is
not dicult to see that a much better
method is making sure that c % p != 0
for all prime numbers p such that p <
c. Using the primes in your ArrayList
this is easy as pie. Note again that
this suggests that you use a while
loop.

I’ve tried to implement both of these methods, and while I get the first one, checking that c % i != 0, I don’t understand the second piece of information saying a better algorithm would be to use c % p !=0. Would this not mean I would have to know all prime numbers to compute the prime number ?

What I have at the moment is as follows :

  public static void isPrime(int candidateNo) {
while (i <= candidateNo/2) {
  if (candidateNo%i==0 && i!=1) {
    return false;
  }
  else
    return true;
}

}

which works, though is woefully inefficient. I am using the function to create an arraylist of prime numbers (if the function returns true, the number is added to the arraylist).

  • 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-18T08:11:02+00:00Added an answer on May 18, 2026 at 8:11 am

    Well, since you are creating a list of all the prime numbers anyway, when you check if a number c is prime, you already have all smaller prime numbers in your list. Right?

    So, instead of testing if any number smaller than candidateNo/2 divides your candidate, you only test if any prime number smaller than candidateNo/2 divides your candidate.

    So, instead of iterating from i = 2 to candidateNo/2, you iterate over the elements of your array list. To this end, this array list should of course be accessible from your isPrime function, so either pass it as an argument or have it as a public static element of your main class.

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

Sidebar

Related Questions

In the web-application I'm developing I currently use a naive solution when connecting to
The naive way of writing building a menu in a Java Swing app is
I have this naive regex "<([\s]|[^<])+?>" (excluding the quotation marks). It seems so straightforward
Perhaps this is a naive question. In my understanding, ASP.NET MVC cannot work with
this may be a naive question, but, as asked in the object, what is
I realize this perhaps a naive question but still I cant figure out how
This seems like perhaps a naive question, but I got into a discussion with
I'm new to C# and .Net in general so this may be a naive
SQLAlchemy's DateTime type allows for a timezone=True argument to save a non-naive datetime object
The naïve FOO = empty_clob() complains about incompatible types. I tried Googling, but (once

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.