📜  稀有号码

📅  最后修改于: 2021-04-24 05:46:08             🧑  作者: Mango

给定整数N ,任务是检查N是否为稀有编号。

例子:

方法:想法是检查N是否为回文数,然后返回false。如果它不是回文,则只需检查N + rev(N)和N – rev(N)是否都是理想平方。
下面是上述方法的实现:

C++
// C++ implementation to check if
// N is a Rare number
#include
using namespace std;
 
 
// Iterative function to
// reverse digits of num
int reversDigits(int num)
{
    int rev_num = 0;
    while(num > 0)
    {
        rev_num = rev_num*10 + num%10;
        num = num/10;
    }
    return rev_num;
}
 
// Function to check if N
// is perfect square
bool isPerfectSquare(long double x)
{  
  // Find floating point value of 
  // square root of x.
  long double sr = sqrt(x);
   
  // If square root is an integer
  return ((sr - floor(sr)) == 0);
}
 
// Function to check if N is an
// Rare number
bool isRare(int N)
{
    // Find reverse of N
    int reverseN = reversDigits(N);
 
    // Number should be non-palindromic
    if(reverseN == N)
        return false;
     
    return isPerfectSquare(N + reverseN) &&
                isPerfectSquare(N - reverseN);
}
 
// Driver Code
int main()
{
    int n = 65;
    if (isRare(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java implementation to check if N
// is a Rare number
class GFG{
 
// Iterative function to
// reverse digits of num
static int reversDigits(int num)
{
    int rev_num = 0;
    while(num > 0)
    {
        rev_num = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}
 
// Function to check if N
// is perfect square
static boolean isPerfectSquare(double x)
{
 
    // Find floating point value of
    // square root of x.
    double sr = Math.sqrt(x);
     
    // If square root is an integer
    return ((sr - Math.floor(sr)) == 0);
}
 
// Function to check if N is an
// Rare number
static boolean isRare(int N)
{
     
    // Find reverse of N
    int reverseN = reversDigits(N);
 
    // Number should be non-palindromic
    if(reverseN == N)
        return false;
     
    return isPerfectSquare(N + reverseN) &&
           isPerfectSquare(N - reverseN);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 65;
 
    if (isRare(n))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by shubham


Python3
# Python3 implementation to check if
# N is a Rare number
import math
 
# Iterative function to
# reverse digits of num
def reversDigits(num):
    rev_num = 0
    while(num > 0):
        rev_num = rev_num * 10 + num % 10
        num = num // 10
 
    return rev_num
 
# Function to check if N
# is perfect square
def isPerfectSquare(x):
     
    # Find floating point value of
    # square root of x.
    sr = math.sqrt(x)
 
    # If square root is an integer
    return ((sr - int(sr)) == 0)
 
# Function to check if N is an
# Rare number
def isRare(N):
     
    # Find reverse of N
    reverseN = reversDigits(N)
 
    # Number should be non-palindromic
    if(reverseN == N):
        return False
 
    return (isPerfectSquare(N + reverseN) and
            isPerfectSquare(N - reverseN))
 
# Driver Code
N = 65
if (isRare(N)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Vishal Maurya


C#
// C# implementation to check if N
// is a Rare number
using System;
class GFG{
  
// Iterative function to
// reverse digits of num
static int reversDigits(int num)
{
    int rev_num = 0;
    while(num > 0)
    {
        rev_num = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}
  
// Function to check if N
// is perfect square
static bool isPerfectSquare(double x)
{
  
    // Find floating point value of
    // square root of x.
    double sr = Math.Sqrt(x);
      
    // If square root is an integer
    return ((sr - Math.Floor(sr)) == 0);
}
  
// Function to check if N is an
// Rare number
static bool isRare(int N)
{
      
    // Find reverse of N
    int reverseN = reversDigits(N);
  
    // Number should be non-palindromic
    if(reverseN == N)
        return false;
      
    return isPerfectSquare(N + reverseN) &&
           isPerfectSquare(N - reverseN);
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 65;
  
    if (isRare(n))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:

Yes

参考文献: OEIS