Write a C program to
Print 1st 30 Perfect numbers starting from 0 and calculate sum.
Hints:
Today the usual definition of a perfect number is in terms of its divisors,
but early definitions were in terms of the 'aliquot parts' of a number.
An
aliquot part of a number is a proper quotient of the
number. So for example the aliquot parts of 10 are 1, 2 and 5. These occur
since 1 =
10/
10, 2 =
10/
5, and 5 =
10/
2.
Note that 10 is not an aliquot part of 10 since it is not a proper quotient,
i.e. a quotient different from the number itself. A perfect number is defined
to be one which is equal to the sum of its aliquot parts.
The four perfect numbers 6, 28, 496 and 8128 seem to have
been known from ancient times and there is no record of these discoveries.
6 = 1 + 2 + 3,
28 = 1 + 2 + 4 + 7 + 14,
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064
If as many numbers as we please beginning
from a unit be set out continuously in double proportion, until the sum of all
becomes a prime, and if the sum multiplied into the last make some number, the
product will be perfect.
Here 'double proportion' means that each number of the
sequence is twice the preceding number. To illustrate this Proposition consider
1 + 2 + 4 = 7 which is prime. Then
(the sum) × (the last) = 7 × 4 = 28,
which is a perfect number. As a second example, 1 + 2 + 4 +
8 + 16 = 31 which is prime. Then 31 × 16 = 496 which is a perfect number.
Now Euclid
gives a rigorous proof of the Proposition and we have the first significant
result on perfect numbers. We can restate the Proposition in a slightly more
modern form by using the fact, known to the Pythagoreans, that
1 + 2 + 4 + ... + 2
k-1 = 2
k -
1.
The Proposition now reads:-
If, for some
k > 1, 2
k - 1 is prime then 2
k-1(2
k
- 1) is a perfect number.
Source Code:
#include<stdio.h>using namespace std;int j,i;
int perfect(){ int sum=0,count=0,sum1=0;
for( j=1; j <= 50000; j++ )
{ i =1;
sum = 0;
while ( i < j )
{ if ( j%i = =0 ) sum = sum+i ;
i++;
} if( sum = = j ) { cout << j <<
endl;
sum1 += j ;
} } cout << "The sum of the first 4 perfect number is: ";
cout <<
endl; return sum1;
}
int main( ){ cout << "First 30 perfect number is:"<<
endl;
cout <<
perfect( ); return 0;
}