![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 3
Rep Power: 0
![]() |
finding primes in an array
I need help writting code that returns primes in an array, one method that returns the nth prime number
int getNthPrimeNumber(int n ){ } and a second that returns the number of prime numbers less than or equal to n int getNumberPrimeNumbers(int n) { } thanks in advance for the help |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2005
Posts: 3
Rep Power: 0
![]() |
thanks but that doesn't really help me I don't know how to chenge it to fit my program.
thanks though |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4
![]() |
All you did was present us with your (likely homework) problem. You didn't tell us how you've already tried to solve the problem or provide us with any code you've started with (besides the function skeletons [that it looks like your teacher gave to you]). You havn't been specific enough either. Like what should the program do if there are no primes at all in the array? Throw an exception, bail, prompt the user, look in another array?
private boolean isPrime(int num) {
int i = 2;
while(i < num) {
int j = num % i;
if(j == 0)
return false;
else
i++;
}
return true;
}
__________________
#if 0 /* in case someone actually tries to compile this */ <Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode? |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2005
Posts: 3
Rep Power: 0
![]() |
thank you for your help, I was able to get the other methods from the is prime method the thing I've been getting hung up on is the math part of isPrime, I had it messed up in a giant if statement which I see how stupid it was, I apologize for leaving such a vague question, and again thanks for the help
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|