Check if a given pair of Numbers are Betrothed numbers or not
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 Betrothed 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 Betrothed 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 doesn’t forms a pair of Betrothed Numbers.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( int N)
{
double sr = sqrt (N);
return (sr - floor (sr)) == 0;
}
void BetrothedNumbers( int n, int m)
{
int Sum1 = 1;
int Sum2 = 1;
for ( int i = 2; i <= sqrt (n); i++) {
if (n % i == 0) {
Sum1 += i
+ (isPerfectSquare(n)
? 0
: n / i);
}
}
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;
}
}
int main()
{
int N = 9504;
int M = 20734;
BetrothedNumbers(N, M);
return 0;
}
|
Java
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" + "\n" );
}
else {
System.out.print( "NO" + "\n" );
}
}
public static void main(String[] args)
{
int N = 9504 ;
int M = 20734 ;
BetrothedNumbers(N, M);
}
}
|
Python3
from math import sqrt,floor
def isPerfectSquare(N):
sr = sqrt(N)
return (sr - floor(sr)) = = 0
def BetrothedNumbers(n,m):
Sum1 = 1
Sum2 = 1
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 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" )
if __name__ = = '__main__' :
N = 9504
M = 20734
BetrothedNumbers(N, M)
|
C#
using System;
class GFG{
static bool 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))
{
Console.Write( "YES" + "\n" );
}
else
{
Console.Write( "NO" + "\n" );
}
}
public static void Main(String[] args)
{
int N = 9504;
int M = 20734;
BetrothedNumbers(N, M);
}
}
|
Javascript
<script>
function isPerfectSquare(N)
{
let sr = Math.sqrt(N);
return (sr - Math.floor(sr)) == 0;
}
function BetrothedNumbers(n, m)
{
let Sum1 = 1;
let Sum2 = 1;
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
Sum1 += i
+ (isPerfectSquare(n)
? 0
: parseInt(n / i));
}
}
for (let i = 2; i <= Math.sqrt(m); i++) {
if (m % i == 0) {
Sum2 += i
+ (isPerfectSquare(m)
? 0
: parseInt(m / i));
}
}
if ((n + 1 == Sum2)
&& (m + 1 == Sum1)) {
document.write( "YES" );
}
else {
document.write( "NO" );
}
}
let N = 9504;
let M = 20734;
BetrothedNumbers(N, M);
</script>
|
Time Complexity: O(?N + ?M)
Auxiliary Space: O(1)
Last Updated :
08 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...