Dado un número entero positivo N , la tarea es comprobar si el número N dado es Sunny Number o no.
Un número N es un número soleado si N + 1 es un cuadrado perfecto .
Ejemplos:
Entrada: N = 8
Salida: Sí
Explicación:
Dado que 9 es un cuadrado perfecto. por lo tanto, 8 es un número soleado.Entrada: N = 11
Salida: No
Explicación:
Dado que 12 no es un cuadrado perfecto. por lo tanto, 8 no es un número soleado.
Planteamiento: La idea es comprobar si (N + 1) es un cuadrado perfecto o no.
A continuación se muestra la implementación del enfoque anterior:
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
Javascript
<script> // Javascript program for the above approach // Function check whether x is a // perfect square or not function isPerfectSquare(x) { // Find floating point value of // square root of x. var sr = Math.sqrt(x); // If square root is an integer return((sr - Math.floor(sr)) == 0); } // Function to check Sunny Number function checkSunnyNumber(N) { // Check if (N + 1) is a perfect // square or not if (isPerfectSquare(N + 1)) { document.write( "Yes"); } // If (N+1) is not a perfect square else { document.write( "No"); } } // Driver Code // Given Number var N = 8; // Function call checkSunnyNumber(N); // This code is contributed by noob2000. </script>
Yes
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por thakurabhaysingh445 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA