📜  加1后检查数字是否可以做平方

📅  最后修改于: 2021-05-28 04:10:11             🧑  作者: Mango

给定整数N ,任务是检查给定数字N加1后是否可以使它成为一个完美的平方。

例子:

方法:通过取n + 1的平方根并检查它是否为整数,来检查n + 1是否为理想平方。如果它是n + 1,则是一个完美的平方,而n是一个晴天数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true
// if x is a 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 that returns true
// if n is a sunny number
bool isSunnyNum(int n)
{
 
    // If (n + 1) is a perfect square
    if (isPerfectSquare(n + 1))
        return true;
    return false;
}
 
// Driver code
int main()
{
    int n = 3;
 
    if (isSunnyNum(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
     
    // Function that returns true
    // if x is a 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 that returns true
    // if n is a sunny number
    static boolean isSunnyNum(int n)
    {
     
        // If (n + 1) is a perfect square
        if (isPerfectSquare(n + 1))
            return true;
        return false;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 3;
     
        if (isSunnyNum(n))
            System.out.println("Yes");
        else
            System.out.println("No");
     
    }
}
 
// This code is contributed by Ryuga


Python3
# Python3 implementation of the approach
import math as mt
 
# Function that returns true
# if x is a perfect square
def isPerfectSquare(x):
 
    # Find floating po value of
    # square root of x
    sr = mt.sqrt(x)
 
    # If square root is an eger
    return ((sr - mt.floor(sr)) == 0)
 
# Function that returns true
# if n is a sunny number
def isSunnyNum(n):
 
    # If (n + 1) is a perfect square
    if (isPerfectSquare(n + 1)):
        return True
    return False
 
# Driver code
n = 3
 
if (isSunnyNum(n)):
    print("Yes")
else:
    print("No")
 
# This code is contributed
# by Mohit Kumar


C#
// C# implementation of the approach
using System;
class GFG
{
     
    // Function that returns true
    // if x is a 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 that returns true
    // if n is a sunny number
    static bool isSunnyNum(int n)
    {
     
        // If (n + 1) is a perfect square
        if (isPerfectSquare(n + 1))
            return true;
        return false;
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 3;
     
        if (isSunnyNum(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by Code_Mech.


PHP


Javascript


输出:
Yes