Pairs of Amicable Numbers
Last Updated :
19 Sep, 2023
Given an array of integers, print the number of pairs in the array that form an amicable pair. Two numbers are amicable if the first is equal to the sum of divisors of the second, and if the second number is equal to the sum of divisors of the first.
Examples :
Input : arr[] = {220, 284, 1184, 1210, 2, 5}
Output : 2
Explanation : (220, 284) and (1184, 1210)
form amicable pair
Input : arr[] = {2620, 2924, 5020, 5564, 6232, 6368}
Output : 3
Explanation : (2620, 2924), (5020, 5564) and (6232, 6368)
forms amicable pair
A simple solution is to traverse each pair and check if they form an amicable pair, if they do we increment the count.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfDiv( int x)
{
int sum = 1;
for ( int i = 2; i <= sqrt (x); i++)
{
if (x % i == 0)
{
sum += i;
if (x / i != i)
sum += x / i;
}
}
return sum;
}
bool isAmicable( int a, int b)
{
return (sumOfDiv(a) == b &&
sumOfDiv(b) == a);
}
int countPairs( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
if (isAmicable(arr[i], arr[j]))
count++;
return count;
}
int main()
{
int arr1[] = { 220, 284, 1184,
1210, 2, 5 };
int n1 = sizeof (arr1) / sizeof (arr1[0]);
cout << countPairs(arr1, n1)
<< endl;
int arr2[] = { 2620, 2924, 5020,
5564, 6232, 6368 };
int n2 = sizeof (arr2) / sizeof (arr2[0]);
cout << countPairs(arr2, n2);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int sumOfDiv( int x)
{
int sum = 1 ;
for ( int i = 2 ; i <= Math.sqrt(x); i++)
{
if (x % i == 0 )
{
sum += i;
if (x / i != i)
sum += x / i;
}
}
return sum;
}
static boolean isAmicable( int a, int b)
{
return (sumOfDiv(a) == b &&
sumOfDiv(b) == a);
}
static int countPairs( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = i + 1 ; j < n; j++)
if (isAmicable(arr[i], arr[j]))
count++;
return count;
}
public static void main(String args[])
{
int arr1[] = { 220 , 284 , 1184 ,
1210 , 2 , 5 };
int n1 = arr1.length;
System.out.println(countPairs(arr1, n1));
int arr2[] = { 2620 , 2924 , 5020 ,
5564 , 6232 , 6368 };
int n2 = arr2.length;
System.out.println(countPairs(arr2, n2));
}
}
|
Python3
def sumOfDiv(x):
sum = 1
for i in range ( 2 , x):
if x % i = = 0 :
sum + = i
return sum
def isAmicable(a, b):
if sumOfDiv(a) = = b and sumOfDiv(b) = = a:
return True
else :
return False
def countPairs(arr, n):
count = 0
for i in range ( 0 , n):
for j in range (i + 1 , n):
if isAmicable(arr[i], arr[j]):
count = count + 1
return count
arr1 = [ 220 , 284 , 1184 ,
1210 , 2 , 5 ]
n1 = len (arr1)
print (countPairs(arr1, n1))
arr2 = [ 2620 , 2924 , 5020 ,
5564 , 6232 , 6368 ]
n2 = len (arr2)
print (countPairs(arr2, n2))
|
C#
using System;
class GFG
{
static int sumOfDiv( int x)
{
int sum = 1;
for ( int i = 2; i <= Math.Sqrt(x); i++)
{
if (x % i == 0)
{
sum += i;
if (x / i != i)
sum += x / i;
}
}
return sum;
}
static bool isAmicable( int a, int b)
{
return (sumOfDiv(a) == b &&
sumOfDiv(b) == a);
}
static int countPairs( int []arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
if (isAmicable(arr[i], arr[j]))
count++;
return count;
}
public static void Main()
{
int []arr1 = {220, 284, 1184,
1210, 2, 5};
int n1 = arr1.Length;
Console.WriteLine(countPairs(arr1, n1));
int []arr2 = {2620, 2924, 5020,
5564, 6232, 6368};
int n2 = arr2.Length;
Console.WriteLine(countPairs(arr2, n2));
}
}
|
PHP
<?php
function sumOfDiv( $x )
{
$sum = 1;
for ( $i = 2; $i <= sqrt( $x ); $i ++)
{
if ( $x % $i == 0)
{
$sum += $i ;
if ( $x / $i != $i )
$sum += $x / $i ;
}
}
return $sum ;
}
function isAmicable( $a , $b )
{
return (sumOfDiv( $a ) == $b and
sumOfDiv( $b ) == $a );
}
function countPairs( $arr , $n )
{
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = $i + 1; $j < $n ; $j ++)
if (isAmicable( $arr [ $i ], $arr [ $j ]))
$count ++;
return $count ;
}
$arr1 = array ( 220, 284, 1184,
1210, 2, 5 );
$n1 = count ( $arr1 );
echo countPairs( $arr1 , $n1 ), "\n" ;
$arr2 = array ( 2620, 2924, 5020,
5564, 6232, 6368 );
$n2 = count ( $arr2 );
echo countPairs( $arr2 , $n2 );
?>
|
Javascript
<script>
function sumOfDiv(x)
{
let sum = 1;
for (let i = 2; i <= Math.sqrt(x); i++)
{
if (x % i == 0)
{
sum += i;
if (parseInt(x / i, 10) != i)
sum += parseInt(x / i, 10);
}
}
return sum;
}
function isAmicable(a, b)
{
return (sumOfDiv(a) == b &&
sumOfDiv(b) == a);
}
function countPairs(arr, n)
{
let count = 0;
for (let i = 0; i < n; i++)
for (let j = i + 1; j < n; j++)
if (isAmicable(arr[i], arr[j]))
count++;
return count;
}
let arr1 = [220, 284, 1184, 1210, 2, 5];
let n1 = arr1.length;
document.write(countPairs(arr1, n1) + "</br>" );
let arr2 = [2620, 2924, 5020, 5564, 6232, 6368];
let n2 = arr2.length;
document.write(countPairs(arr2, n2));
</script>
|
An efficient solution is to keep the numbers stored in a map and for every number, we find the sum of its proper divisor and check if that’s also present in the array. If it is present, we can check if they form an amicable pair or not.
Thus, the complexity would be considerably reduced. Below is the C++ program for the same.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfDiv( int x)
{
int sum = 1;
for ( int i = 2; i <= sqrt (x); i++)
{
if (x % i == 0) {
sum += i;
if (x / i != i)
sum += x / i;
}
}
return sum;
}
bool isAmicable( int a, int b)
{
return (sumOfDiv(a) == b &&
sumOfDiv(b) == a);
}
int countPairs( int arr[], int n)
{
unordered_set< int > s;
int count = 0;
for ( int i = 0; i < n; i++)
s.insert(arr[i]);
for ( int i = 0; i < n; i++)
{
if (s.find(sumOfDiv(arr[i])) != s.end())
{
int sum = sumOfDiv(arr[i]);
if (isAmicable(arr[i], sum))
count++;
}
}
return count / 2;
}
int main()
{
int arr1[] = { 220, 284, 1184,
1210, 2, 5 };
int n1 = sizeof (arr1) / sizeof (arr1[0]);
cout << countPairs(arr1, n1)
<< endl;
int arr2[] = { 2620, 2924, 5020,
5564, 6232, 6368 };
int n2 = sizeof (arr2) / sizeof (arr2[0]);
cout << countPairs(arr2, n2)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int sumOfDiv( int x)
{
int sum = 1 ;
for ( int i = 2 ; i <= Math.sqrt(x); i++)
{
if (x % i == 0 )
{
sum += i;
if (x / i != i)
sum += x / i;
}
}
return sum;
}
static boolean isAmicable( int a, int b)
{
return (sumOfDiv(a) == b &&
sumOfDiv(b) == a);
}
static int countPairs( int arr[], int n)
{
HashSet<Integer> s = new HashSet<Integer>();
int count = 0 ;
for ( int i = 0 ; i < n; i++)
s.add(arr[i]);
for ( int i = 0 ; i < n; i++)
{
if (s.contains(sumOfDiv(arr[i])))
{
int sum = sumOfDiv(arr[i]);
if (isAmicable(arr[i], sum))
count++;
}
}
return count / 2 ;
}
public static void main(String[] args)
{
int arr1[] = { 220 , 284 , 1184 ,
1210 , 2 , 5 };
int n1 = arr1.length;
System.out.println(countPairs(arr1, n1));
int arr2[] = { 2620 , 2924 , 5020 ,
5564 , 6232 , 6368 };
int n2 = arr2.length;
System.out.println(countPairs(arr2, n2));
}
}
|
Python3
import math
def sumOfDiv(x):
sum = 1 ;
for i in range ( 2 , int (math.sqrt(x))):
if x % i = = 0 :
sum + = i
if i ! = x / i:
sum + = x / i
return int ( sum );
def isAmicable(a, b):
return (sumOfDiv(a) = = b and
sumOfDiv(b) = = a)
def countPairs(arr,n):
s = set ()
count = 0
for i in range (n):
s.add(arr[i])
for i in range (n):
if sumOfDiv(arr[i]) in s:
sum = sumOfDiv(arr[i])
if isAmicable(arr[i], sum ):
count + = 1
return int (count / 2 );
arr1 = [ 220 , 284 , 1184 ,
1210 , 2 , 5 ]
n1 = len (arr1)
print (countPairs(arr1, n1))
arr2 = [ 2620 , 2924 , 5020 ,
5564 , 6232 , 6368 ]
n2 = len (arr2)
print (countPairs(arr2, n2))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int sumOfDiv( int x)
{
int sum = 1;
for ( int i = 2; i <= Math.Sqrt(x); i++)
{
if (x % i == 0)
{
sum += i;
if (x / i != i)
sum += x / i;
}
}
return sum;
}
static Boolean isAmicable( int a, int b)
{
return (sumOfDiv(a) == b &&
sumOfDiv(b) == a);
}
static int countPairs( int []arr, int n)
{
HashSet< int > s = new HashSet< int >();
int count = 0;
for ( int i = 0; i < n; i++)
s.Add(arr[i]);
for ( int i = 0; i < n; i++)
{
if (s.Contains(sumOfDiv(arr[i])))
{
int sum = sumOfDiv(arr[i]);
if (isAmicable(arr[i], sum))
count++;
}
}
return count / 2;
}
public static void Main(String[] args)
{
int []arr1 = { 220, 284, 1184,
1210, 2, 5 };
int n1 = arr1.Length;
Console.WriteLine(countPairs(arr1, n1));
int []arr2 = { 2620, 2924, 5020,
5564, 6232, 6368 };
int n2 = arr2.Length;
Console.WriteLine(countPairs(arr2, n2));
}
}
|
Javascript
<script>
function sumOfDiv(x)
{
let sum = 1;
for (let i = 2; i <= Math.sqrt(x); i++)
{
if (x % i == 0)
{
sum += i;
if (x / i != i)
sum += x / i;
}
}
return sum;
}
function isAmicable(a, b)
{
return (sumOfDiv(a) == b &&
sumOfDiv(b) == a);
}
function countPairs(arr, n)
{
let s = new Set();
let count = 0;
for (let i = 0; i < n; i++)
s.add(arr[i]);
for (let i = 0; i < n; i++)
{
if (s.has(sumOfDiv(arr[i])))
{
let sum = sumOfDiv(arr[i]);
if (isAmicable(arr[i], sum))
count++;
}
}
return Math.floor(count / 2);
}
let arr1 = [ 220, 284, 1184,
1210, 2, 5 ];
let n1 = arr1.length;
document.write(countPairs(arr1, n1) + "<br/>" );
let arr2 = [ 2620, 2924, 5020,
5564, 6232, 6368 ];
let n2 = arr2.length;
document.write(countPairs(arr2, n2) + "<br/>" );
</script>
|
This article is contributed by Ashutosh Kumar
Share your thoughts in the comments
Please Login to comment...