Sunny Number
Last Updated :
07 Jun, 2021
Given a positive integer N, the task is to check whether the given number N is Sunny Number or not.
A number N is a sunny number if N + 1 is a perfect square.
Examples:
Input: N = 8
Output: Yes
Explanation:
Since 9 is a perfect square. therefore 8 is a sunny number.
Input: N = 11
Output: No
Explanation:
Since 12 is not a perfect square. therefore 8 is not a sunny number.
Approach: The idea is to check whether (N + 1) is a perfect square or not.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
bool isPerfectSquare( long double x)
{
long double sr = sqrt (x);
return ((sr - floor (sr)) == 0);
}
void checkSunnyNumber( int N)
{
if (isPerfectSquare(N + 1)) {
cout << "Yes\n" ;
}
else {
cout << "No\n" ;
}
}
int main()
{
int N = 8;
checkSunnyNumber(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean isPerfectSquare( double x)
{
double sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0 );
}
static void checkSunnyNumber( int N)
{
if (isPerfectSquare(N + 1 ))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
public static void main(String[] args)
{
int N = 8 ;
checkSunnyNumber(N);
}
}
|
Python3
from math import *
def isPerfectSquare(x):
sr = sqrt(x)
return ((sr - floor(sr)) = = 0 )
def checkSunnyNumber(N):
if (isPerfectSquare(N + 1 )):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
N = 8
checkSunnyNumber(N)
|
C#
using System;
class GFG {
static bool isPerfectSquare( double x)
{
double sr = Math.Sqrt(x);
return ((sr - Math.Floor(sr)) == 0);
}
static void checkSunnyNumber( int N)
{
if (isPerfectSquare(N + 1))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
public static void Main(String[] args)
{
int N = 8;
checkSunnyNumber(N);
}
}
|
Javascript
<script>
function isPerfectSquare(x)
{
var sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0);
}
function checkSunnyNumber(N)
{
if (isPerfectSquare(N + 1)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
var N = 8;
checkSunnyNumber(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...