Given two positive numbers N and M, the task is to check whether the given pairs of numbers (N, M) form a Betrothed Numbers or not.
Examples:
Input: N = 48, M = 75
Output: Yes
Explanation:
The proper divisors of 48 are 1, 2, 3, 4, 6, 8, 12, 16, 24
Sum of proper divisors of 48 is 75(sum1)
The proper divisors of 75 are 1, 3, 5, 15, 25
Sum of proper divisors of 48 is 49(sum2)
Since sum2 = N + 1, therefore the given pairs form berothered numbers.Input: N = 95, M = 55
Output: No
Explanation:
The proper divisors of 95 are 1, 5, 19
Sum of proper divisors of 48 is 25(sum1)
The proper divisors of 55 are 1, 5, 11
Sum of proper divisors of 48 is 17(sum2)
Since Neither sum2 is equals N + 1 nor sum1 is equals to M + 1, therefore the given pairs doesn’t form berothered numbers.
Approach:
- Find the sum of proper divisors of the given numbers N and M.
- If sum of proper divisors of N is equals to M + 1 or sum of proper divisors of M is equals to N + 1 then the given pairs form a Betrothed Numbers.
- Else it doen’t forms a pair of Betrothed Numbers.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check whether N is // Perfect Square or not bool isPerfectSquare(int N) { // Find sqrt double sr = sqrt(N); return (sr - floor(sr)) == 0; } // Function to check whether the given // pairs of numbers is Betrothed Numbers // or not void BetrothedNumbers(int n, int m) { int Sum1 = 1; int Sum2 = 1; // For finding the sum of all the // divisors of first number n for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) { Sum1 += i + (isPerfectSquare(n) ? 0 : n / i); } } // For finding the sum of all the // divisors of second number m for (int i = 2; i <= sqrt(m); i++) { if (m % i == 0) { Sum2 += i + (isPerfectSquare(m) ? 0 : m / i); } } if ((n + 1 == Sum2) && (m + 1 == Sum1)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } // Driver Code int main() { int N = 9504; int M = 20734; // Function Call BetrothedNumbers(N, M); return 0; } |
Java
// Java program for the above approach class GFG{ // Function to check whether N is // Perfect Square or not static boolean isPerfectSquare(int N) { // Find sqrt double sr = Math.sqrt(N); return (sr - Math.floor(sr)) == 0; } // Function to check whether the given // pairs of numbers is Betrothed Numbers // or not static void BetrothedNumbers(int n, int m) { int Sum1 = 1; int Sum2 = 1; // For finding the sum of all the // divisors of first number n for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { Sum1 += i + (isPerfectSquare(n) ? 0 : n / i); } } // For finding the sum of all the // divisors of second number m for (int i = 2; i <= Math.sqrt(m); i++) { if (m % i == 0) { Sum2 += i + (isPerfectSquare(m) ? 0 : m / i); } } if ((n + 1 == Sum2) && (m + 1 == Sum1)) { System.out.print("YES" +"\n"); } else { System.out.print("NO" +"\n"); } } // Driver Code public static void main(String[] args) { int N = 9504; int M = 20734; // Function Call BetrothedNumbers(N, M); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program for the above approach from math import sqrt,floor # Function to check whether N is # Perfect Square or not def isPerfectSquare(N): # Find sqrt sr = sqrt(N) return (sr - floor(sr)) == 0 # Function to check whether the given # pairs of numbers is Betrothed Numbers # or not def BetrothedNumbers(n,m): Sum1 = 1 Sum2 = 1 # For finding the sum of all the # divisors of first number n for i in range(2,int(sqrt(n))+1,1): if (n % i == 0): if (isPerfectSquare(n)): Sum1 += i else: Sum1 += i + n/i # For finding the sum of all the # divisors of second number m for i in range(2,int(sqrt(m))+1,1): if (m % i == 0): if (isPerfectSquare(m)): Sum2 += i else: Sum2 += i + (m / i) if ((n + 1 == Sum2) and (m + 1 == Sum1)): print("YES") else: print("NO") # Driver Code if __name__ == '__main__': N = 9504 M = 20734 # Function Call BetrothedNumbers(N, M) # This code is contributed by Surendra_Gangwar |
C#
// C# program for the above approach using System; class GFG{ // Function to check whether N is // perfect square or not static bool isPerfectSquare(int N) { // Find sqrt double sr = Math.Sqrt(N); return (sr - Math.Floor(sr)) == 0; } // Function to check whether the given // pairs of numbers is Betrothed numbers // or not static void BetrothedNumbers(int n, int m) { int Sum1 = 1; int Sum2 = 1; // For finding the sum of all the // divisors of first number n for(int i = 2; i <= Math.Sqrt(n); i++) { if (n % i == 0) { Sum1 += i + (isPerfectSquare(n) ? 0 : n / i); } } // For finding the sum of all the // divisors of second number m for(int i = 2; i <= Math.Sqrt(m); i++) { if (m % i == 0) { Sum2 += i + (isPerfectSquare(m) ? 0 : m / i); } } if ((n + 1 == Sum2) && (m + 1 == Sum1)) { Console.Write("YES" + "\n"); } else { Console.Write("NO" + "\n"); } } // Driver Code public static void Main(String[] args) { int N = 9504; int M = 20734; // Function Call BetrothedNumbers(N, M); } } // This code is contributed by Rajput-Ji |
NO
Time Complexity: O(√N + √M)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Betrothed numbers
- Find the sum of all Betrothed numbers up to N
- Check if the given two numbers are friendly pair or not
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Check if there is any pair in a given range with GCD is divisible by k
- Check if quantities of 3 distinct colors can be converted to a single color by given merge-pair operations
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Check whether the given numbers are Cousin prime or not
- Pair of prime numbers with a given sum and minimum absolute difference
- Print a pair of numbers with the given Sum and Product
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Check for Amicable Pair
- Check whether a given point lies inside a triangle or not
- Check if given number is Emirp Number or not
- Check whether a given matrix is orthogonal or not
- Check if a given matrix is Hankel or not
- Check whether a given number is Polydivisible or Not
- Check if the given 2-D points form T-shape or not
- Check whether a given Number is Power-Isolated or not
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

