944,045 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1414
  • C++ RSS
Nov 15th, 2006
0

explanation

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
batista06 is offline Offline
5 posts
since Oct 2006
Nov 15th, 2006
0

Re: explanation

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Nov 16th, 2006
0

Re: explanation

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.
Reputation Points: 22
Solved Threads: 11
Posting Whiz in Training
DavidRyan is offline Offline
229 posts
since Jul 2006
Nov 16th, 2006
0

Re: explanation

Click to Expand / Collapse  Quote originally posted by DavidRyan ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Nov 16th, 2006
0

Re: explanation

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.
Reputation Points: 22
Solved Threads: 11
Posting Whiz in Training
DavidRyan is offline Offline
229 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need some help with Student Grade program
Next Thread in C++ Forum Timeline: adjacency lists





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC