• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Finding Prime #'s? (Java programming)

NTB

Diamond Member
I recieved an assignment in my Java class recently to write a program that will find & display a bunch of prime numbers. I know what a prime number is, and I know how to find them, given a deffinite range (between 1 and x) and if I can do it on my own - writing it out on a piece of paper, or something similar - but I'm not quite sure how to implement something like this in a program. Here are the specific instructions:

Write a program that meets the following requirements:

1)Declare a method to determine whether an integer is a prime number. Use the flowing method declaration:

public static boolean isPrime(int num)

2) Use the isPrime method to find first 1000 prime numbers, and display every 10 prime numbers in a row as follows:

2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71 ...

I'm stumped 😕

Nate
 
There are lots of algorithms out there for determining if a number is prime. You should be able to translate one into Java pretty easily.
 
boolean isPrime(int num){
for (i = 2; i <= num/2; i++){
if(num % i == 0){
return "0";
}
return 1;
}

Come on, that's about as easy as any programming gets.
 
Originally posted by: notfred
boolean isPrime(int num){
for (i = 2; i <= num/2; i++){
if(num % i == 0){
return "0";
}
return 1;
}

Come on, that's about as easy as any programming gets.

You'll want to return true or false. And you actually only need to test up the square root of num. Should save some iterations.
 
I did this for fun in a class once ... used the Sieve of Eratosthenes. I got it to work up to about 3 million, but at that point my hasty programming job was too inefficient. Anyways, generate an list as long as you want to find prime numbers (if you want to find up to 100, create an list of 100, filled with those values). For every value of two or more still existing, try to divide each following link by that amount. If it divides with remainder zero, the number is composite, so remove it from the list. Go to the next value, etc. Once you've reached the end of the list (or beyond sqrt(n), where n is the limit), you're done, and the remaining elements are prime.
That's not a terribly good explanation, it's not my strong point.
Here's a better link I found on google ... and it has a Java app
Sieve of Eras ....

And for the record, in the list implementation I used recursion, and blew the hell out of the stack. Later I used an array, and hopped through it.

...Actually, reading through the problem description, you can't use this method. This generates the list of primes, instead of checking a list to find which are prime. Oh well. Sorry.
 
Back
Top