Check if a given pair of Numbers are Betrothed numbers or not | Java program for the above approach ; Function to check whether N is Perfect Square or not ; Find sqrt ; Function to check whether the given pairs of numbers is Betrothed Numbers or not ; For finding the sum of all the divisors of first number n ; For finding the sum of all the divisors of second number m ; Driver Code ; Function Call
class GFG { static boolean isPerfectSquare ( int N ) { double sr = Math . sqrt ( N ) ; return ( sr - Math . floor ( sr ) ) == 0 ; } static void BetrothedNumbers ( int n , int m ) { int Sum1 = 1 ; int Sum2 = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { Sum1 += i + ( isPerfectSquare ( n ) ? 0 : n / i ) ; } } 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 " + "NEW_LINE"); } else { System . out . print ( " NO " + "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 9504 ; int M = 20734 ; BetrothedNumbers ( N , M ) ; } }