Check whether the number can be made perfect square after adding 1 Last Updated : 16 Oct, 2022 Comments Improve Suggest changes Like Article Like Report Given an integer N, the task is to check whether N the given number can be made a perfect square after adding 1 to it. Examples: Input: 3 Output: Yes 3 + 1 = 4 which is a perfect square i.e. 22 Input: 5 Output: No 5 + 1 = 6 which is not a perfect square. Approach: Check whether n + 1 is a perfect square or not by taking the square root of n + 1 and checking whether it is an integer. If it is then n + 1 is a perfect square and n is a sunny number. Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> 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 <?php // PHP implementation of the approach // Function that returns true // if x is a perfect square function 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 that returns true // if n is a sunny number function isSunnyNum($n) { // If (n + 1) is a perfect square if (isPerfectSquare($n + 1)) return true; return false; } // Driver code $n = 3; if (isSunnyNum($n)) echo "Yes"; else echo "No"; // This code is contributed // by Akanksha Rai ?> JavaScript <script> // Javascript implementation of the approach // Function that returns true // if x is a perfect square function isPerfectSquare(x) { // Find floating point value of // square root of x let 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 function isSunnyNum(n) { // If (n + 1) is a perfect square if (isPerfectSquare(n + 1)) return true; return false; } // Driver code let n = 3; if (isSunnyNum(n)) document.write("Yes"); else document.write("No"); // This code is contributed by rishavmahato348 </script> Output: Yes Time Complexity: O(logn)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check whether the number can be made perfect square after adding 1 V Vaibhav_Arora Follow Improve Article Tags : Mathematical C Programs DSA school-programming Practice Tags : Mathematical Similar Reads Check whether the number can be made perfect square after adding K Given two numbers N and K, the task is to check whether the given number N can be made a perfect square after adding K to it.Examples: Input: N = 7, K = 2 Output: Yes Explanation: 7 + 2 = 9 which is a perfect square. Input: N = 5, K = 3 Output: No Explanation: 5 + 3 = 8 which is not a perfect square 4 min read Check whether a number can be represented as difference of two squares Given a number N, the task is to check if this number can be represented as the difference of two perfect squares or not. Examples: Input: N = 3 Output: Yes Explanation: 22 - 11 = 3 Input: N = 10 Output: No Approach: The idea is that all the numbers can be represented as the difference of two square 6 min read Check whether a number can be represented by the product of two squares Given an integer n, our task is to check whether number n can be represented by the product of two squares. If it is possible then print "yes" otherwise print "no".Examples : Input: n = 144 Output: Yes Explanation: The given number 144 can be represented as 22 * 62 = 144. Input: n = 25 Output: No Ex 7 min read Check whether a number can be represented by sum of two squares We have number n. We need to find whether number n can be represented by the sum of two squares.Examples : Input : n = 17Output : Yes4^2 + 1^2 = 17Input : n = 169Output : Yes5^2 + 12^2 = 169Input : n = 24Output : NoBrute-force approach - O(n) We use two for loops running till the square root of n an 15+ min read Find the Next perfect square greater than a given number Given a number N, the task is to find the next perfect square greater than N.Examples: Input: N = 6Output: 9Explanation: 9 is a greater number than 6 and is also a perfect square Input: N = 9Output: 16 Approach: Find the square root of given N.Calculate its floor value using floor function in C++.Th 2 min read Check whether the number formed by concatenating two numbers is a perfect square or not Given two numbers a and b and the task is to check whether the concatenation of a and b is a perfect square or not.Examples: Input: a = 1, b = 21 Output: Yes 121 = 11 Ã 11, is a perfect square. Input: a = 100, b = 100 Output: No 100100 is not a perfect square. Approach: Initialize the number as stri 4 min read Largest factor of a given number which is a perfect square Given a number N . The task is to find the largest factor of that number which is a perfect square.Examples: Input : N = 420Output : 4Input : N = 100Output : 100A Simple Solution is to traverse all of the numbers in decreasing order from the given number down till 1 and if any of these numbers is a 10 min read Find the Largest N digit perfect square number in Base B Given two integers N and B, the task is to find the largest N digit numbers of Base B which is a perfect square.Examples: Input: N = 2, B = 10 Output: 81 Explanation: 81 is the largest 2-digit perfect square in base 10.Input: N = 1, B = 8 Output: 4 Explanation: 4 is the largest 1 digit Octal number 9 min read Number of times the largest perfect square number can be subtracted from N Given a number N. At every step, subtract the largest perfect square( ? N) from N. Repeat this step while N > 0. The task is to count the number of steps that can be performed. Examples: Input: N = 85 Output: 2 First step, 85 - (9 * 9) = 4 Second step 4 - (2 * 2) = 0 Input: N = 114 Output: 4 Firs 7 min read Find minimum number to be divided to make a number a perfect square Given a positive integer n. Find the minimum number which divide n to make it a perfect square.Examples: Input : n = 50 Output : 2 By Dividing n by 2, we get which is a perfect square. Input : n = 6 Output : 6 By Dividing n by 6, we get which is a perfect square. Input : n = 36 Output : 1 A number i 6 min read Like