| | |
explanation
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2006
Posts: 5
Reputation:
Solved Threads: 0
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)
int perfect(int num,int den) { int rem = num - ((num/den)*den); if (rem==0) return(den); else return(0); }
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
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.
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.
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.
•
•
•
•
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
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.
•
•
•
•
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.
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.
![]() |
Similar Threads
- Need explanation for reported negative sum (C++)
- would like an explanation (Site Layout and Usability)
- Narue's Heap sort a further explanation? (C)
- Explanation of Process File (Java)
- Hosts file question about Gorilla's explanation (Viruses, Spyware and other Nasties)
- array declaration explanation needed (C)
Other Threads in the C++ Forum
- Previous Thread: Need some help with Student Grade program
- Next Thread: adjacency lists
Views: 1210 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






