Amicable Program

CMPC :: Computing :: Programming :: C++

Go down

Amicable Program

Post by Admin on Wed Dec 02, 2009 12:47 am

Amicable numbers are two different numbers so related that the sum of the proper divisors of one of the numbers is equal to the other. (A proper divisor of a number is a positive integer divisor other than the number itself. For example, the proper divisors of 6 are 1, 2, and 3.) A pair of amicable numbers constitutes an aliquot sequence of period 2. A related concept is that of a perfect number, which is a number which equals the sum of its own proper divisors, in other words a number which forms an aliquot sequence of period 1.

For example, the smallest pair of amicable numbers is (220, 284); for the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71, and 142, of which the sum is 220.

that's from the Wikipedia page about Amicable pairs.

Now here is the code Very Happy

Hope you enjoy it and understand it!

Code:
#include <iostream>

using namespace std;

//Only factors of a number

int sumFactors(long number) //Find the sum of the factors of the given number (divisors)

{
   long factor = 0, sum = 0;
   
   for (long i = 1; i < (number / 2 + 1); i++)
   {

      if (!(number % i)){
      factor = i;
      sum += factor;
      }

   }

return sum;
}

int main(){
   long x,y,z;

   x=220;
   
   cout<<"Amicable Number Pairs: \n\n";
   
   while(0<1){
   
      y=sumFactors(x);
      z=sumFactors(y);
      
         if(z==x && x < y){
            cout<<"["<<x<<","<<y<<"] \n";
         }

      x++;

   }


system("pause");
return 0;
}
avatar
Admin
Pirate King

Posts : 559

Back to top Go down

Back to top

- Similar topics

CMPC :: Computing :: Programming :: C++

 
Permissions in this forum:
You cannot reply to topics in this forum