Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 27th, 2005, 3:21 PM   #1
victor_shade
Newbie
 
Join Date: Oct 2005
Posts: 3
Rep Power: 0 victor_shade is on a distinguished road
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
victor_shade is offline   Reply With Quote
Old Oct 27th, 2005, 4:08 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
http://www.programmingforums.org/for...ighlight=prime
__________________
"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
Mjordan2nd is offline   Reply With Quote
Old Oct 27th, 2005, 4:55 PM   #3
victor_shade
Newbie
 
Join Date: Oct 2005
Posts: 3
Rep Power: 0 victor_shade is on a distinguished road
thanks but that doesn't really help me I don't know how to chenge it to fit my program.

thanks though
victor_shade is offline   Reply With Quote
Old Oct 27th, 2005, 5:13 PM   #4
2roll4life7
Programmer
 
2roll4life7's Avatar
 
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4 2roll4life7 is on a distinguished road
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;
}
I'm providing you with one of the functions I've written that you'll obviously need. I left it uncommmented on purpose. If you can't figure out the rest of what you need to make those functions work, I suggest you take a different class.
__________________
#if 0 /* in case someone actually tries to compile this */
- libpng version 1.2.8 (example.c)

<Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode?
2roll4life7 is offline   Reply With Quote
Old Oct 27th, 2005, 5:48 PM   #5
victor_shade
Newbie
 
Join Date: Oct 2005
Posts: 3
Rep Power: 0 victor_shade is on a distinguished road
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
victor_shade is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:40 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC