Download Perfect number

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Prime number wikipedia , lookup

Bernoulli number wikipedia , lookup

Mersenne prime wikipedia , lookup

0.999... wikipedia , lookup

Transcript
Perfect number
"Perfect and beautiful objects are always rare, hence we should not expect perfect
numbers in abundance".
Nicomachos, I/II century author of "Introduction to Arithmetic",
Perfect numbers had always been a mystical notion for ancient Greeks and
Egyptians and they were assigned miraculous properties. The names of
distinguished persons who dealt with perfect numbers are worth mentioning:
Pythagoras, Euclid, Nicomachos, St. Augustin, Hudalrichus Regius, Cataldi, Fermat,
Mersenne and Euler.
So far, 44 perfect numbers are known.
Definition
Perfect number is a natural number that is the sum of its proper positive divisors.
In the above definition a notion of proper divisor was introduced, the notion that is
worth knowing in order to fully understand perfect numbers. A proper divisor of n is a
positive divisor of n which is different from n.
Thus, the smallest perfect number is 6 because 6 = 3 + 2 + 1.
The following ones:
28 = 14 + 7 + 4 + 2 + 1, 496 = 1 + 2 + 4 + 8 + 1 6 + 31 + 62 + 124 + 248, 8128= 1 + 2
+ 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064
and more: 33550336, 137438691328,...
For example, number 27 is not a perfect number as the proper divisors of 27 are
numbers: 1, 3, 9 and their sum, 13, does not equal 27. Let’s create an algorithm that
will check if a given number is a perfect number.
Problem specification
Input data:
n ϵ N – ends of an interval, n>1.
Output data:
Perfect numberϵ N.
Helper variables:
i ϵ N – iteration variable
Flowchart
Start, sum, give, print, stop
The implementation below is an extension of the above algorithm. The program finds
perfect numbers in a given interval [a,b] where numbers a,b ϵ N, a > 1 and a < b.
Although the program completes the task, the program is not very effective and does
not cope with finding larger perfect numbers (exceeding the type int).
Source file
View/Hide
give ends of the interval
The program above completes the task quickly and correctly, and finds perfect
numbers in the interval [2,10000]. If we extended the interval to 1 000 000, the
program would execute approximately 10 times to the power of 12 operations. Thus,
an average computer with such defined data would complete this task in about 6 min.
Hopefully, the program can be improved so that the task is completed much quicker.
Below is a more efficient version of the program perfectn.cpp
View/Hide
Give ends of the interval
As you can see the program has quickened the calculation significantly.
To practice, I suggest you try to improve the program perfect2.cpp further and check
whether or not the changes quicken the algorithm. Suggestions for the perfect2.cpp
improvements:
1.
currently:
for (int i=2; i*i<=n; i++)
if (n%i == 0) sum += i+n/i;
change:
long double root = sqrt(n);
for (int i=2; i<=root; i++)
if (n%i == 0) sum += i+n/i;
2.
Currently:
for (int i=2; i*i<=n; i++)
if (n%i == 0) sum += i+n/i;
Change:
long double root = n;
while (fabs(root*root-n)>0.5) root = 0.5*(root+n/root);
for (int i=2; i<=root; i++)
if (n%i == 0) sum += i+n/i;
For the ambitious ones, I recommend an analysis of the outcome of the program
perfect3.cpp which measures the time of three algorithms. The algorithms were
previously modified in terms of finding a perfect number in a given interval. To notice
the difference in time in these algorithms I suggest applying the interval [2,1000000]
and waiting.
View/Hide
Euclidean method
First recorded contemplations about perfect numbers appear in Euclidean Elements
about 300 BC. There is a theorem that is very essential for our work on perfect
numbers.
Theorem
If we take any finite set of prime numbers the first of which is 1 and each subsequent
is two times larger than the preceding one, and add all of them up, then if the product
of this addition is a prime number, the number multiplied by a number two times
larger than the last one in the set will be a perfect number.
The theorem implies that one needs to calculate the sums of subsequent powers of
2, for example 1 + 2 + 4 + 8 +... If any of the sums turns out to be a prime number,
one needs to multiply it by the last component which will give us a perfect number.
e.g.: 1 + 2 = 22 − 1 = 3 where 3 is a prime number
Thus, because 2 · 3 = 6, then 6 is a prime number.
1 + 2 + 4, leads to another perfect number 4 · 7 = 28 etc.
It turns out that in this way each perfect number is even. In other words, each even
perfect number takes the form:
where the first factor of this product is a prime number.
So far it is unknown whether there are any odd perfect numbers.
Let’s write a program that will use the formula above and find a perfect number.
Source code
View/Hide
// Perfect number Euclidean formula
// perfectn4.cpp
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
long long d, p;
bool ok=1;
for (int i=1; i<50; i++)
{
ok = 1;
p = pow(2.0,i)-1;
// checking if number p is a prime number
for (int j=2; j<=sqrt(p); j++)
if (p%j==0)
{
ok = 0;
break;
}
if (ok==1)
{
d=p*pow(2.0,i-1);
cout << d << endl;
}
}
return 0;
}
Outcome of the program
Exercise 5.3
View/Hide
Write a program that for the first four perfect numbers will check the multiplicative inverse
for a number equals 2 e.g.
Exercise 5.4
View/Hide
Write a program that for the first four perfect numbers will check the following theorem:
All even perfect numbers are hexagonal i.e. each of them is a sum of subsequent natural
numbers beginning with number 1. E.g.
where 3, 7, 31... Mersenne’s prime numbers.