explanation

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 5
Reputation: batista06 is an unknown quantity at this point 
Solved Threads: 0
batista06 batista06 is offline Offline
Newbie Poster

explanation

 
0
  #1
Nov 15th, 2006
I stumbled on this function that calculates #'s that are perfect, how ever I do not understand why it works. any one care to explain?

  1. int perfect(int num,int den)
  2. {
  3. int rem = num - ((num/den)*den);
  4. if (rem==0)
  5. return(den);
  6. else
  7. return(0);
  8. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: explanation

 
0
  #2
Nov 15th, 2006
pick any number and calculate it with pencil & paper. For example use 6 and 2.

6/2 = 3; --> (num/den)
3 * 2 = 6 --> ((num/den) * den)
rem = 6 - 6 == 0 --> rem = num - ((num/den)*den);

so 6 is a perfect number.

now try it with 7 and 2
7/2 = 3
3 * 2 = 6
rem = 7 - 6 == 1
so 7 is not a perfect number
Last edited by Ancient Dragon; Nov 15th, 2006 at 11:36 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 229
Reputation: DavidRyan is an unknown quantity at this point 
Solved Threads: 11
DavidRyan's Avatar
DavidRyan DavidRyan is offline Offline
Posting Whiz in Training

Re: explanation

 
0
  #3
Nov 16th, 2006
A prefect number is an integer which is the sum of it's proper positive divisors. EG, the proper positive divisors of the integer 6 are 1, 2 and 3 (the number itself is not a proper divisor) and 6 = 1 + 2 + 3, therefore 6 is a perfect number.
If that is what you mean by "numbers that are perfect", the code you posted doesn't do that. In fact, the code which you posted will, in all cases, return 0, because:
rem = num - ((num/den)*den)
rem = num - (num*(den/den))
rem = num - (num*1)
rem = num - num
rem = 0 in all cases


If you are trying to find out whether a number is perfect or not, try searching the forum.
Last edited by DavidRyan; Nov 16th, 2006 at 12:31 am.
Please anyone, correct me if I am wrong. It is the best way for me to learn.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: explanation

 
0
  #4
Nov 16th, 2006
Originally Posted by DavidRyan View Post
In fact, the code which you posted will, in all cases, return 0, because:
rem = num - ((num/den)*den)
rem = num - (num*(den/den))
rem = num - (num*1)
rem = num - num
rem = 0 in all cases
mathematically you are right, but it doesn't work that way in a computer programs which do integer division. Look at the example I posted where num = 7 and den = 2. The result is not 0. (7/2)*2) == 3 * 2 == 6. Remainders are always discarded, there is no rounding.

The function posted in by the OP only indicates whether num is evenly divisible by den, and not whether it is a perfect number or not. The mod operator % will provide the same answer as that function.
Last edited by Ancient Dragon; Nov 16th, 2006 at 1:23 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 229
Reputation: DavidRyan is an unknown quantity at this point 
Solved Threads: 11
DavidRyan's Avatar
DavidRyan DavidRyan is offline Offline
Posting Whiz in Training

Re: explanation

 
0
  #5
Nov 16th, 2006
Originally Posted by Ancient Dragon View Post
mathematically you are right, but it doesn't work that way in a computer programs which do integer division. Look at the example I posted where num = 7 and den = 2. The result is not 0. (7/2)*2) == 3 * 2 == 6. Remainders are always discarded, there is no rounding.
Ah, so essentially all are rounded down. I didn't realise that, as I've bugger all experience in C/C++ ("Hello, world!" is about it) and Java will just give an error if you try int a = 7/2.
Last edited by DavidRyan; Nov 16th, 2006 at 2:16 am.
Please anyone, correct me if I am wrong. It is the best way for me to learn.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1210 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC