C Programming Reference

C Programming Reference >> C Programming Short Notes >>

22/07/07 - 2224 Views - Ratings :   4.64 of 5 / 11 Votes


Perfect Number List Production Using A C Program

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.

//Recommmended Compiler - DEV C++ Dont Try Turbo C++ your system may crash
//Also use extension .cpp than c to access cout instead of printf
             
#define CAP 500000 //Set This value according to yourself you may hurt your computer
#define ARRAYSTORAGE 160000 
// Change Caps Value To Increase Search Limit.
// Change ArrayStorage To Increase Array Size, can be limited by compiler especially older ones.
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <stdlib.h>
#include <iostream.h>
unsigned long long int counter;//Keep Count of number of items in storage
unsigned long long int liststorage [ARRAYSTORAGE];  //Can store upto ARRAYSTORAGE MACRO VALUE Prime Numbers
void primelist ()
 {
   printf ("Generating Prime List, Time taken depends upons CAP VALUE, Please wait ....");
   unsigned long long int i,j;
   for (i = 3; i < CAP; i++)   // Loops from 3 to UPPERLIMIT
    {
      for (j = 0; j < counter; j++)
         {
          if (!(i%liststorage[j])) break; // Number is Prime
          if (j == counter-1) //Number is Not Prime
           {
             liststorage [counter] = i;//Add Item To List
             counter++;//Increment Item Counter
             if (counter > ARRAYSTORAGE)
              {
                printf ("\n\nArray Overrun .... Please Allocate more memmory to Array");
                getch ();
                exit (0);
               }
              break;
           }
        }    
    }
 
 printf ("\n\nNumber of Primes In List : %d " ,counter);
 
 }
int bsearch (int num) //Binary search function returns 1 if num exist and 0 otherwise
 {
   unsigned long long int mid,lower,upper;
   lower = 0;
   upper = counter;
   for ( mid = (lower+upper)/2; lower <= upper; mid = (lower + upper)/2)
      {
         if ( liststorage [mid] == num)
           {
             return 1;
           }
         if (liststorage [mid] > num)
           upper = mid - 1;
         else
           lower = mid +1;
       }
    return 0;
 }
unsigned long long int power (int input) // ouputs a power of 2;
 {
   unsigned long long int ouput = 1;
   int i;
   for (i = 0; i<input;i++)
     {
       ouput *= 2;
     }
   if (ouput <= 0)
     {
       printf ("\n\nUnsigned long long int range overrun ... this is the largest value in C, beyond this c is helpless");
       getch ();
       exit (0);
     }
   return ouput;
}
void perfectlist ()
 {
   printf ("\n\nPerfect Number List : ");
   unsigned long long int i;
   for (i = 0;i < counter; i++)
      {
        unsigned long long int check1 = power (liststorage [i]);
        if (check1 > CAP)
            {
              printf ("\n\nSeach Limit Overrun ....Increase search limit");
              getch ();
              exit (0);
            }
        if (bsearch (check1 -1)) //That is 2^n - 1 is a prime
           {
              cout << (check1/2)* (check1 - 1)<<endl;
           }
        }
  }
             
int main ()
{
  counter = 1;  // Intialize Counter
  liststorage [0] = 2; // Put 2 in the List
  //clrscr (); //uncomment if using turbo C/C++
  primelist ();
  perfectlist ();
  getch ();
  return 0;
}
             
 

Download Source

Reader Comments -

Author Comments
Add Comments 


Name :    
Reply :   


Rating :

Code :
Code

 

 
© 2006 cencyclopedia.com