Check if the given two numbers are friendly pair or not
Last Updated :
23 Jun, 2022
Given two positive integer N, M. The task is to check if N and M are friendly pair or not.
In number theory, friendly pairs are two numbers with a common abundancy index, the ratio between the sum of divisors of a number and the number itself i.e ?(n)/n. S o, two number n and m are friendly number if
?(n)/n = ?(m)/m.
where ?(n) is the sum of divisors of n.
Examples:
Input : n = 6, m = 28
Output : Yes
Explanation:
Divisor of 6 are 1, 2, 3, 6.
Divisor of 28 are 1, 2, 4, 7, 14, 28.
Sum of divisor of 6 and 28 are 12 and 56
respectively. Abundancy index of 6 and 28
are 2. So they are friendly pair.
Input : n = 18, m = 26
Output : No
The idea is to find the sum of divisor of n and m. And to check if the abundancy index of n and m, we will find the Greatest Common Divisors of n and m with their sum of divisors. And check if the reduced form of abundancy index of n and m are equal by checking if their numerator and denominator are equal or not. To find the reduced form, we will divide numerator and denominator by GCD.
Below is the implementation of above idea :
C++
#include <bits/stdc++.h>
using namespace std;
int sumofFactors( int n)
{
int res = 1;
for ( int i = 2; i <= sqrt (n); i++) {
int count = 0, curr_sum = 1;
int curr_term = 1;
while (n % i == 0) {
count++;
n = n / i;
curr_term *= i;
curr_sum += curr_term;
}
res *= curr_sum;
}
if (n >= 2)
res *= (1 + n);
return res;
}
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
bool checkFriendly( int n, int m)
{
int sumFactors_n = sumofFactors(n);
int sumFactors_m = sumofFactors(m);
int gcd_n = gcd(n, sumFactors_n);
int gcd_m = gcd(m, sumFactors_m);
if (n / gcd_n == m / gcd_m &&
sumFactors_n / gcd_n == sumFactors_m / gcd_m)
return true ;
else
return false ;
}
int main()
{
int n = 6, m = 28;
checkFriendly(n, m) ? (cout << "Yes\n" ) :
(cout << "No\n" );
return 0;
}
|
Java
class GFG {
static int sumofFactors( int n)
{
int res = 1 ;
for ( int i = 2 ; i <= Math.sqrt(n); i++) {
int count = 0 , curr_sum = 1 ;
int curr_term = 1 ;
while (n % i == 0 ) {
count++;
n = n / i;
curr_term *= i;
curr_sum += curr_term;
}
res *= curr_sum;
}
if (n >= 2 )
res *= ( 1 + n);
return res;
}
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static boolean checkFriendly( int n, int m)
{
int sumFactors_n = sumofFactors(n);
int sumFactors_m = sumofFactors(m);
int gcd_n = gcd(n, sumFactors_n);
int gcd_m = gcd(m, sumFactors_m);
if (n / gcd_n == m / gcd_m &&
sumFactors_n / gcd_n == sumFactors_m / gcd_m)
return true ;
else
return false ;
}
public static void main (String[] args)
{
int n = 6 , m = 28 ;
if (checkFriendly(n, m))
System.out.print( "Yes\n" );
else
System.out.print( "No\n" );
}
}
|
Python3
import math
def sumofFactors(n):
res = 1
for i in range ( 2 , int (math.sqrt(n)) + 1 ):
count = 0 ; curr_sum = 1 ; curr_term = 1
while (n % i = = 0 ):
count + = 1
n = n / / i
curr_term * = i
curr_sum + = curr_term
res * = curr_sum
if (n > = 2 ):
res * = ( 1 + n)
return res
def gcd(a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def checkFriendly(n, m):
sumFactors_n = sumofFactors(n)
sumFactors_m = sumofFactors(m)
gcd_n = gcd(n, sumFactors_n)
gcd_m = gcd(m, sumFactors_m)
if (n / / gcd_n = = m / / gcd_m and
sumFactors_n / / gcd_n = = sumFactors_m / / gcd_m):
return True
else :
return False
n = 6 ; m = 28
if (checkFriendly(n, m)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static int sumofFactors( int n)
{
int res = 1;
for ( int i = 2; i <= Math.Sqrt(n); i++)
{
int count = 0, curr_sum = 1;
int curr_term = 1;
while (n % i == 0)
{
count++;
n = n / i;
curr_term *= i;
curr_sum += curr_term;
}
res *= curr_sum;
}
if (n >= 2)
res *= (1 + n);
return res;
}
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static bool checkFriendly( int n, int m)
{
int sumFactors_n = sumofFactors(n);
int sumFactors_m = sumofFactors(m);
int gcd_n = gcd(n, sumFactors_n);
int gcd_m = gcd(m, sumFactors_m);
if (n / gcd_n == m / gcd_m &&
sumFactors_n / gcd_n ==
sumFactors_m / gcd_m)
return true ;
else
return false ;
}
public static void Main (String[] args)
{
int n = 6, m = 28;
if (checkFriendly(n, m))
Console.Write( "Yes\n" );
else
Console.Write( "No\n" );
}
}
|
PHP
<?php
function sumofFactors( $n )
{
$res = 1;
for ( $i = 2; $i <= sqrt( $n ); $i ++)
{
$count = 0;
$curr_sum = 1;
$curr_term = 1;
while ( $n % $i == 0)
{
$count ++;
$n = $n / $i ;
$curr_term *= $i ;
$curr_sum += $curr_term ;
}
$res *= $curr_sum ;
}
if ( $n >= 2)
$res *= (1 + $n );
return $res ;
}
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function checkFriendly( $n , $m )
{
$sumFactors_n = sumofFactors( $n );
$sumFactors_m = sumofFactors( $m );
$gcd_n = gcd( $n , $sumFactors_n );
$gcd_m = gcd( $m , $sumFactors_m );
if ( $n / $gcd_n == $m / $gcd_m and
$sumFactors_n / $gcd_n ==
$sumFactors_m / $gcd_m )
return true;
else
return false;
}
$n = 6;
$m = 28;
if (checkFriendly( $n , $m ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function sumofFactors(n)
{
let res = 1;
for (let i = 2; i <= Math.sqrt(n); i++) {
let count = 0, curr_sum = 1;
let curr_term = 1;
while (n % i == 0) {
count++;
n = n / i;
curr_term *= i;
curr_sum += curr_term;
}
res *= curr_sum;
}
if (n >= 2)
res *= (1 + n);
return res;
}
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function checkFriendly(n, m)
{
let sumFactors_n = sumofFactors(n);
let sumFactors_m = sumofFactors(m);
let gcd_n = gcd(n, sumFactors_n);
let gcd_m = gcd(m, sumFactors_m);
if (n / gcd_n == m / gcd_m &&
sumFactors_n / gcd_n == sumFactors_m / gcd_m)
return true ;
else
return false ;
}
let n = 6, m = 28;
if (checkFriendly(n, m))
document.write( "Yes\n" );
else
document.write( "No\n" );
</script>
|
Output
Yes
Time Complexity: O(?n log 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...