|
C Programming Reference >> C Programming Short Notes >> 22/07/07 - 2224 Views - Ratings :
Level - Advance
Well before i begin i want to say that this task is real tough. Tough not because it involves untouched features of c but because it involves a very complicated logic. The processing required in this job is so much that efficiency of the code cannot be neglected otherwise you will hang your pc for even the smallest initial values. Perfect number mathematically is defined as an positive integer who in itself is a sum of its factors not including the number itself. Such as 6 = 1 + 2 + 3, so 6 is a perfect number. Next Perfect number are 28,496,128. Now you can see that amount of calculation needed is too large if we just try a loop then check for factors for desire number. In this way if you are not using a sumper computer than you will surely hang your computer. Also its very intresting that an odd perfect number is never been seen. No one has ever proved or rejected its existence. So there is a nobel prize waiting for you with your name written all over it. Biggest hinderance which can occur is that the numbers involved will shoot out of the range of the data type defined in C so we cannt go much deeper yet we can find some no doubt. Infact i am sure this program will take a lot of time to find 5th perfect number which is an astounding value of 8 digits 33550336 Since loops or recursion can be disasterous we will use a diffrent approach. Unlike prime numbers, perfect numbers do follow a pattern. This was given by Euclid, a great mathematician who devoted his life studying these perfect numbers as i plan to devote mine to programming. It is observed that 2^n-1 (2^n - 1) will give a perfect number if and only if (2^n - 1) is prime. So we will try to produce a list by feeding each value of n. Also to improve efficiency we will use concept that for (2^n-1) to be prime n should be a prime number so we will feed on prime number values of n not every value. Also we will be using code of list of prime number production develop in this article. Also we will be using binary search to find a value in storage list since it will be in sorted order in which case efficiency of binary search is unmatched. Also will be using Cout function of C++, to our aid because printf is not capable of printing such large values. Also it is recommended that you use modern compiler like DevC++ than Turbo C++ because they are larger bit compilers a nd can handle the load. Source code below is written in C and is implementation of concept above, It produces a list of perfect numbers as output.
Reader Comments -
|
|||