Rare Numbers
Last Updated :
12 Apr, 2023
Given an integer N, the task is to check if N is a Rare Number.
Rare Number is a number N which is non-palindromic and N+rev(N) and N-rev(N) are both perfect squares where rev(N) is the reverse of the number N. For Example rev(65) = 56
Examples:
Input: N = 65
Output: Yes
65 – 56 = 9 and 65 + 56 = 121 are both perfect squares
Input: N = 10
Output: No
Approach: The idea is to check if N is a palindromic number, then return false. And if it is non-palindromic then just check whether N + rev(N) and N – rev(N) are both perfect squares or not.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int reverseDigits( int num)
{
int rev_num = 0;
while (num > 0)
{
rev_num = rev_num*10 + num%10;
num = num/10;
}
return rev_num;
}
bool isPerfectSquare( long double x)
{
long double sr = sqrt (x);
return ((sr - floor (sr)) == 0);
}
bool isRare( int N)
{
int reverseN = reverseDigits(N);
if (reverseN == N)
return false ;
return isPerfectSquare(N + reverseN) &&
isPerfectSquare(N - reverseN);
}
int main()
{
int n = 65;
if (isRare(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static int reverseDigits( int num)
{
int rev_num = 0 ;
while (num > 0 )
{
rev_num = rev_num * 10 + num % 10 ;
num = num / 10 ;
}
return rev_num;
}
static boolean isPerfectSquare( double x)
{
double sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0 );
}
static boolean isRare( int N)
{
int reverseN = reverseDigits(N);
if (reverseN == N)
return false ;
return isPerfectSquare(N + reverseN) &&
isPerfectSquare(N - reverseN);
}
public static void main(String[] args)
{
int n = 65 ;
if (isRare(n))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math
def reverseDigits(num):
rev_num = 0
while (num > 0 ):
rev_num = rev_num * 10 + num % 10
num = num / / 10
return rev_num
def isPerfectSquare(x):
sr = math.sqrt(x)
return ((sr - int (sr)) = = 0 )
def isRare(N):
reverseN = reverseDigits(N)
if (reverseN = = N):
return False
return (isPerfectSquare(N + reverseN) and
isPerfectSquare(N - reverseN))
N = 65
if (isRare(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int reverseDigits( int num)
{
int rev_num = 0;
while (num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
static bool isPerfectSquare( double x)
{
double sr = Math.Sqrt(x);
return ((sr - Math.Floor(sr)) == 0);
}
static bool isRare( int N)
{
int reverseN = reverseDigits(N);
if (reverseN == N)
return false ;
return isPerfectSquare(N + reverseN) &&
isPerfectSquare(N - reverseN);
}
public static void Main(String[] args)
{
int n = 65;
if (isRare(n))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function reverseDigits( num)
{
let rev_num = 0;
while (num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = parseInt(num / 10);
}
return rev_num;
}
function isPerfectSquare( x)
{
let sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0);
}
function isRare( N)
{
let reverseN = reverseDigits(N);
if (reverseN == N)
return false ;
return isPerfectSquare(N + reverseN) && isPerfectSquare(N - reverseN);
}
let n = 65;
if (isRare(n)) {
document.write( "Yes" );
} else {
document.write( "No" );
}
</script>
|
Output:
Yes
Time Complexity: O(N1/2)
References: OEIS
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...