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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:06:02+00:00 2026-05-18T07:06:02+00:00

I’ve been given a description of how I am to implement a prime number

  • 0

I’ve been given a description of how I am to implement a prime number generator using Java; but I’m not sure if I am doing it properly. I was hoping someone could give me some general advice for how to improve my implementation! Please don’t give me an answer as this is homework and I’d like to crack this myself/become a better programmer/not cheat !

I will post the description and my solution below, basically I would just like to know some general things – have I followed the instructions properly (most important as I find the language used a little confusing) and if I could make my program any better (if so some general tips would be nice so I can figure out how…I think my use of the logarithm is way off, but I can’t figure out a better way to use it)!

Apologies if this isn’t allowed – if it isn’t then please ignore or delete ! Thank you very much for your help! 🙂

Description :

Prime( int initialCapacity ): The class
constructor. The purpose of the
constructor is to compute and store
the first initialCapacity prime �first initialCapacity primefirst initialCapacity primefirst i
numbers. The idea is that you compute
the numbers once and store them so
that you can re-use them several
times. You should use an ArrayList to
store the prime numbers. An algorithm
for computing prime numbers is
provided in the next section.

  1. Start with an empty list of prime numbers. You may represent this list
    using an ArrayList.

  2. While the size of the ArrayList is not equal to n, consider the next
    candidate prime number. Initially, the
    next candidate prime number should be
    2, but otherwise it should be the
    successor of the previous candidate
    prime number.

  3. If the candidate prime number is a prime then add it to the list.

  4. Continue with Step 2.

Note that this algorithm suggests that you
should use a while loop. Step 3 of the
previous algorithm requires that you
know how to decide whether a given
candidate prime, c > 1, is a prime
number. As a matter of fact, the
denition of prime numbers already
suggests a naive algorithm. For
example, you could use a while loop to
check that c %i 6= 0 for all positive
integers i such that 1 < i and i < c.
However, it is not dicult to see that
a much better method is making sure
that c % p 6= 0 for all prime numbers
p such that p < c. Using the primes in
your ArrayList this is easy as pie.is
method is the method which you are
supposed to use. Note again that this
suggest that you use a while loop.
Finally, your implementation should be
ecient and should not waste checks of
the form c % p 6= 0 or c % p = 0. So,
you should stop cheking as soon as c %
p = 0 for some of the primes in
ArrayList.

My solution is as follows :

import java.util.*;
public class Prime {

  public static void main( String[] args ){

  }
  public static void prime( int initialCapacity){
    int index=2;
    int logOfInitialCapacity = initialCapacity / (int)(Math.log(initialCapacity));
    ArrayList<Integer> listOfPrimeNumbers = new ArrayList<Integer>(logOfInitialCapacity);
    boolean[] isPrimeNumber = new boolean[initialCapacity + 1]; // boolean defaults to
    // false

    for (int i=0;i==initialCapacity;i++) {
        isPrimeNumber[index] = true;
      }
      while ( index <= listOfPrimeNumbers.size() )
      {
        if (isPrimeNumber[index]) {
          listOfPrimeNumbers.add(index);
        }
      for (int j = index; j * index <= initialCapacity; j++) {
          isPrimeNumber[index * j] = false;
      }
         // Now mark the multiple of i as non-prime number
        index++;
      }
  }

}

which should compute the first initialCapacity of prime numbers and save them in an arrayList as the question asks…I’m just not sure if I have done exactly as the question says. It’s probably more a product of studying all day and being a little worn out than anything else !

Thanks a lot for the help and for reading all of this; sorry for the long post.

  • 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-18T07:06:03+00:00Added an answer on May 18, 2026 at 7:06 am

    Your solution doesn’t really match the problem description.

    The problem description prescribes primality testing (i.e. given a candidate prime, test whether it is prime). It also wants initialCapacity primes.

    The problems with your solution:

    1. it seems to be trying to perform sieving
    2. it is inefficient, because the multiples of a composite number are crossed out.
    3. it finds the primes up to initialCapacity (not the first initialCapacity primes – to see the difference, try initialCapacity = 1).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

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.