  Given two positive integer N, M. The task is to if N and M are friendly pair or not.

In theory, pairs are two numbers with a common abundancy index, the ratio between the sum of divisors of a and the itself i.e σ(n)/n. S o, two n and m are friendly 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  and 56
respectively. Abundancy index of 6 and 28
are 2. So they are friendly .

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 C++ implementation of this approach:

// Check if the given two number
// are friendly pair or not.
#include <bits/stdc++.h>
using namespace std;

// Returns sum of all factors of n.
int sumofFactors(int n)
{

// Traversing through all prime factors.
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++;

// THE BELOW STATEMENT MAKES
// IT BETTER THAN ABOVE METHOD
// AS WE REDUCE VALUE OF n.
n = n / i;

curr_term *= i;
curr_sum += curr_term;
}

res *= curr_sum;
}

// This condition is to handle
// the case when n is a prime
// number greater than 2.
if (n >= 2)
res *= (1 + n);

return res;
}

// Function to return gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}

// Function to check if the given two
// number are friendly pair or not.
bool checkFriendly(int n, int m)
{
// Finding the sum of factors of n and m
int sumFactors_n = sumofFactors(n);
int sumFactors_m = sumofFactors(m);

// finding gcd of n and sum of its factors.
int gcd_n = gcd(n, sumFactors_n);

// findig gcd of m and sum of its factors.
int gcd_m = gcd(m, sumFactors_m);

// checking is numerator and denominator of
// abundancy index of both number are equal
// or not.
if (n / gcd_n == m / gcd_m &&
sumFactors_n / gcd_n == sumFactors_m / gcd_m)
return true;

else
return false;
}

// Driver code
int main()
{
int n = 6, m = 28;
checkFriendly(n, m) ? (cout << "Yesn") :
(cout << "Non");
return 0;
}

Output

Yes If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

SHARE
Previous articleBioCatch raises \$30m