📜  阳光号码

📅  最后修改于: 2021-04-29 08:36:08             🧑  作者: Mango

给定正整数N ,任务是检查给定数字N是否为Sunny Number。

例子:

方法:想法是检查(N +1)是否是一个完美的正方形。
下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
// Function check whether x is a
// perfect square or not
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 Sunny Number
void checkSunnyNumber(int N)
{
 
    // Check if (N + 1) is a perfect
    // square or not
    if (isPerfectSquare(N + 1)) {
        cout << "Yes\n";
    }
 
    // If (N+1) is not a perfect square
    else {
        cout << "No\n";
    }
}
 
// Driver Code
int main()
{
 
    // Given Number
    int N = 8;
 
    // Function call
    checkSunnyNumber(N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
     
// Function check whether x is a
// perfect square or not
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 Sunny Number
static void checkSunnyNumber(int N)
{
 
    // Check if (N + 1) is a perfect
    // square or not
    if (isPerfectSquare(N + 1))
    {
        System.out.println("Yes");
    }
 
    // If (N+1) is not a perfect square
    else
    {
        System.out.println("No");
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Number
    int N = 8;
 
    // Function call
    checkSunnyNumber(N);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
from math import *
 
# Function check whether x is a
# perfect square or not
def isPerfectSquare(x):
     
    # Find floating point value of
    # square root of x.
    sr = sqrt(x)
 
    # If square root is an integer
    return((sr - floor(sr)) == 0)
 
# Function to check Sunny Number
def checkSunnyNumber(N):
     
    # Check if (N + 1) is a perfect
    # square or not
    if (isPerfectSquare(N + 1)):
        print("Yes")
 
    # If (N+1) is not a perfect square
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
     
    # Given Number
    N = 8
 
    # Function call
    checkSunnyNumber(N)
 
# This code is contributed by Bhupendra_Singh


C#
// C# program for the above approach
using System;
 
class GFG {
     
// Function check whether x is
// a perfect square or not
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 sunny number
static void checkSunnyNumber(int N)
{
     
    // Check if (N + 1) is a perfect
    // square or not
    if (isPerfectSquare(N + 1))
    {
        Console.WriteLine("Yes");
    }
 
    // If (N+1) is not a perfect square
    else
    {
        Console.WriteLine("No");
    }
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given Number
    int N = 8;
 
    // Function call
    checkSunnyNumber(N);
}
}
 
// This code is contributed by Rohit_ranjan


输出:
Yes

时间复杂度: O(1)

辅助空间: O(1)