Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jun 2nd, 2006, 1:39 PM   #1
cairo
Newbie
 
Join Date: Dec 2005
Posts: 18
Rep Power: 0 cairo is on a distinguished road
prime number, problem

bool isPrime(int num)
{
	bool isPrimeNum;

	int sqrtNum;
	int divisor = 3;

	if (num == 2)		   	
		isPrimeNum = true;
	else
		if (num % 2==0)
			isPrimeNum=false;
		else
		{
			isPrimeNum = true;

			sqrtNum = static_cast<int>(pow(static_cast<double>(num), 0.5));
			while (divisor <= num )
			{
				if (num % divisor == 0 )
				{
					isPrimeNum = false;
					cout << " not prime number"<<endl;
					break;
				}
				else
					divisor = divisor + 2;
			}
		}

		return isPrimeNum;
}


im going to crack my head because of this program, i failed to solve the problem, i cant print out the " is not prime " message.
any idea, guys ?
cairo is offline   Reply With Quote
Old Jun 2nd, 2006, 2:17 PM   #2
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
Is there any reason for the sqrtNum variable, i cant see it doing anything.
NSchnarr is offline   Reply With Quote
Old Jun 2nd, 2006, 2:25 PM   #3
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
When I ran your code, the cout worked fine.

And your code as of now works backwards. It just thinks primes are not primes, and vise versa.

Last edited by Twilight; Jun 2nd, 2006 at 2:47 PM.
Twilight is offline   Reply With Quote
Old Jun 2nd, 2006, 2:43 PM   #4
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
A few things.

in your loop, while ( divisor <= num ) means that if you chose, say 37 and your divisor got to 37, the loop would execute and state that it is not a prime when it is. You need to change `num` to `sqrtNum` or `<=` to `<`

If you are testing with even numbers, the cout will never get called:
if (num % 2==0)
	isPrimeNum=false;

If you want to make it much more efficient for large numbers, you could compile a list of primes up to 2^16 (by cut and paste/download from the internet) and check against that list up to the square root of the number. It would use more memory, but hardly touch the cpu. If it's not important, this would probably be a waste of time.

EDIT: Also try putting your num % xxx in parenthesis(sp?).
Harakim 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 7:33 AM.

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