Check for Amicable Pair
Last Updated :
23 Jun, 2022
Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself.
Examples:
Input : x = 220, y = 284
Output : Yes
Proper divisors of 220 are 1, 2, 4, 5,
10, 11, 20, 22, 44, 55 and 110. Sum of
these is 284. Proper divisors of 284
are 1, 2, 4, 71 and 142 with sum 220.
Input : 1 2
Output :No
The logic is very simple. We compare sum of the proper divisors of both numbers and compare sum for one number with other number.
C++
#include <bits/stdc++.h>
using namespace std;
int divSum( int n)
{
int result = 0;
for ( int i = 2; i <= sqrt (n); i++)
{
if (n % i == 0)
{
if (i == (n / i))
result += i;
else
result += (i + n/i);
}
}
return (result + 1);
}
bool areAmicable( int x, int y)
{
if (divSum(x) != y)
return false ;
return (divSum(y) == x);
}
int main() {
int x = 220, y = 284;
if (areAmicable(x, y))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int divSum( int n)
{
int result = 0 ;
for ( int i = 2 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
return (result + 1 );
}
static boolean areAmicable( int x, int y)
{
if (divSum(x) != y)
return false ;
return (divSum(y) == x);
}
public static void main (String[] args)
{
int x = 220 , y = 284 ;
if (areAmicable(x, y))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
import math
def divSum(n) :
result = 0
for i in range ( 2 , int (math.sqrt(n)) + 1 ) :
if (n % i = = 0 ) :
if (i = = int (n / i)) :
result = result + i
else :
result = result +
(i + int (n / i))
return (result + 1 )
def areAmicable(x, y) :
if (divSum(x) ! = y) :
return False
return (divSum(y) = = x)
x = 220
y = 284
if (areAmicable(x, y)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static int divSum( int n)
{
int result = 0;
for ( int i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
return (result + 1);
}
static bool areAmicable( int x, int y)
{
if (divSum(x) != y)
return false ;
return (divSum(y) == x);
}
public static void Main ()
{
int x = 220, y = 284;
if (areAmicable(x, y))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function divSum( $n )
{
$result = 0;
for ( $i = 2; $i <= sqrt( $n ); $i ++)
{
if ( $n % $i == 0)
{
if ( $i == ( $n / $i ))
$result += $i ;
else
$result += ( $i + $n / $i );
}
}
return ( $result + 1);
}
function areAmicable( $x , $y )
{
if (divSum( $x ) != $y )
return false;
return (divSum( $y ) == $x );
}
$x = 220;
$y = 284;
if (areAmicable( $x , $y ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function divSum(n)
{
let result = 0;
for (let i = 2; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
return (result + 1);
}
function areAmicable(x, y)
{
if (divSum(x) != y)
return false ;
return (divSum(y) == x);
}
let x = 220;
let y = 284;
if (areAmicable(x, y))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output:
Yes
Time Complexity: O(?n)
Auxiliary Space: O(1)
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...