Code that is supposed to find perfect numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Busgosu
    New Member
    • Nov 2009
    • 2

    Code that is supposed to find perfect numbers

    i dont know how to upload in a way that you can see it clearly.. so im just going to copy and paste what i have so far... sorry. :S

    Code:
    #include <iostream>
    
    using namespace std;
    
    #include <cmath>
    
    void calculateNumber (double);
    double mypow (double, int);
    
    int main ()
    {
    	
    	double x = 2 ;
    	int y = 2;
    	double myNumber= 0; 
    
    	
    	myNumber = mypow ( x , y );
    
    	calculateNumber(myNumber);
    
    return 0;
    }
    
    
    double mypow ( double x1, int y1)
    {
    	
    	double x = 2;
    
    	int perfectNumber = 0;
    
    	
    	
    
    	for (; perfectNumber <=1000; perfectNumber++)
    	{
    		for (int z = 2; z <perfectNumber; z++)
    		{
    	
    			for (int y = 2 ; y < perfectNumber; y++)
    			{
    				if (perfectNumber ==  ((pow (x,z - 1)) * ((pow (x, y)) - 1)))
    				 cout<< " " <<perfectNumber<<endl<<endl;
    			}
    		}
    	}
    
    
    	 
    
    return perfectNumber;
    }
    
    void calculateNumber (double perfectNumber)
    { 
    	//double perfectNumber;
    	cout << " " << perfectNumber;
    
    
    }
    thanks for your help.. the program outputs many numbers that arent actually perfect numbers.. :S
    Last edited by Banfa; Nov 10 '09, 04:58 PM. Reason: Added [Code] ... [/code] tags
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    What do you think is not a perfect number but the program lists it as such?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      It's probably here:

      Code:
      if (perfectNumber ==  ((pow (x,z - 1)) * ((pow (x, y)) - 1))
      The pow function returns a double. The compiler will promote perfectNumber from int to double and then do the ==. Unfortunately, you can't use the == operator with floating point.

      A little Google on floating point arithmetic will give you a lot of info.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Originally posted by Busgosu
        Code:
        for (perfectNumber=0; perfectNumber <=1000; perfectNumber++)
        {
            for (int z=2; z<perfectNumber; z++)
            {
                for (int y=2 ; y<perfectNumber; y++)
                {
                    if (perfectNumber ==  ((pow (2, z-1)) * ((pow (2, y)) - 1)))
                        cout<< " " <<perfectNumber<<endl<<endl;
                }
            }
        }
        I'm not familiar with this algorithm for finding perfect numbers. Where did you get it from? I suggest you do a little more research into the mathematics of perfect numbers before you worry about getting the source code right.

        It looks to me like you are trying to make use of the relationship between Mersenne primes and perfect numbers known as the Euclid-Euler Theorem. However, be warned that this relationship only applies to even perfect numbers. You need to use some other technique to account for odd perfect numbers. (Or are you allowed to take advantage of the consensus that if there are any odd perfect numbers they must all be greater than 10^300?)

        By the way, you don't need the pow() function to raise "2" to a power. It is much easier to do that with shift-left. By avoiding the pow() function you bypass the floating-point issues raised by weaknessforcats .

        By the way, there are only three perfect numbers less than 1000.

        Comment

        • Busgosu
          New Member
          • Nov 2009
          • 2

          #5
          my mistake is that i didnt take into account the Mersenne primes. The second part of the equation (((pow (2, y)) - 1))) has to be a prime number, since i did not tell the program that it had to be a prime number it outputs many values that are not perfect numbers.
          ( i know my english is not perfect, i am not english, i am from Spain)

          Comment

          Working...