Amicable Numbers

amicableAmicable Numbers Definition

Two positive integer numbers (m, n) are amicable if the sum of the proper divisors of (m) is equal to (n) and the sum of the proper divisors of (n) is equal to (m). A proper divisor of a number does not include the number itself. For example, the proper divisors of (4) are 1 and 2 only.

Amicable Numbers Examples

( 220   ,   284 )
( 1184  ,  1210 )
( 2620  ,  2924 )
( 5020  ,  5564 )
( 6232  ,  6368 )
( 10744 , 10856 )
( 12285 , 14595 )
( 17296 , 18416 )
( 63020 , 76084 )
( 66928 , 66992 )

Amicable Numbers C++

//Includes
#include <iostream>

//STD
using namespace std;

//Check if n and m are amicable
int isAmicable(int n, int m)
{
    //Sum of (n) proper divisors
    int ns = 0;
    //Sum of (m) proper divisors
    int ms = 0;
    
    //Boolean: 1 if the sum of (n) proper divisors equals to (m)
    int nb = 0;
    //Boolean: 1 if the sum of (m) proper divisors equals to (n)
    int mb = 0;
    
    //Find all proper divisors of (n) and calculate the sum
    for (int i = 1; i < n; i++)
    {
        if (n % i == 0)
        {
            ns = ns + i;
        }
    }
    
    //This must be true if the pair are amicable
    if (ns == m)
    {
        nb = 1;
    }

    //Find all proper divisors of (m) and calculate the sum
    for (int j = 1; j < m; j++)
    {
        if (m % j == 0)
        {
            ms = ms + j;
        }
    }
    
    //This must be true if the pair are amicable
    if (ms == n)
    {
        mb = 1;
    }
    
    //If both conditions are true then (n) and (m) are amicable
    if (nb == 1 && mb == 1)
    {
        return 1;
    }
    else
    {
        return 0;   
    }
}

//Main function
int main()
{
    //Sample numbers
    int n = 5020;
    int m = 5564;
    
    //Check if they are amicable
    if (isAmicable(n, m) == 1)
    {
        cout << "YES : (" << n << "," << m << ")";
    }
    else
    {
        cout << "NO : (" << n << "," << m << ")";
    }
	
	return 0;
}

Amicable Numbers Under 10000

If you want to print amicable numbers list under 10000 then you need to have two nested loops. The first loop goes from i = 1 to 10000 and the second loop goes from j = i + 1 to 1000. There is no need to go from 1 to 10000 in each loop otherwise there is going to be a lot of repetitions. Note also that these two nested loops are going to take a long time to run for larger numbers of (n) and (m).

Amicable Numbers Program in Java

//Main class
public class Amicable 
{
 //Main function
 public static void main(String[] args) 
 {
  //Sample numbers
  int n = 5020;
  int m = 5564;

  //Check if they are amicable
  if (isAmicable(n, m) == 1) 
  {
   System.out.println("YES : (" + n + "," + m + ")");
  } 
  else 
  {
   System.out.println("NO : (" + n + "," + m + ")");
  }
 }

 //Check if n and m are amicable
 static int isAmicable(int n, int m) 
 {
  //Sum of (n) proper divisors
  int ns = 0;

  //Sum of (m) proper divisors
  int ms = 0;

  //Boolean: 1 if the some of (n) proper divisors equals to (m)
  int nb = 0;
  //Boolean: 1 if the some of (m) proper divisors equals to (n)
  int mb = 0;

  //Find all proper divisors of (n) and calculate the sum
  for (int i = 1; i < n; i++) 
  {
   if (n % i == 0) 
   {
    ns = ns + i;
   }
  }

  //This must be true if the pair are amicable
  if (ns == m) 
  {
   nb = 1;
  }

  //Find all proper divisors of (m) and calculate the sum
  for (int j = 1; j < m; j++) 
  {
   if (m % j == 0) 
   {
    ms = ms + j;
   }
  }

  //This must be true if the pair are amicable
  if (ms == n) 
  {
   mb = 1;
  }

  //If both conditions are true then (n) and (m) are amicable
  if (nb == 1 && mb == 1) 
  {
   return 1;
  } 
  else 
  {
   return 0;
  }
 }
}

Leave a Reply

%d bloggers like this: