Given an integer N, the task is to find the smallest and the largest N digit numbers which are also perfect squares.
Input: N = 2
Output: 16 81
16 and 18 are the smallest and the largest 2-digit perfect squares.
Input: N = 3
Output: 100 961
Approach: For increasing values of N starting from N = 1, the series will go on like 9, 81, 961, 9801, ….. for the largest N-digit perfect square whose Nth term will be pow(ceil(sqrt(pow(10, N))) – 1, 2).
And 1, 16, 100, 1024, ….. for the smallest N-digit perfect square whose Nth term will be pow(ceil(sqrt(pow(10, N – 1))), 2).
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Largest sub-array whose all elements are perfect squares
- Largest number from the longest set of anagrams possible from all perfect squares of length K
- Find smallest perfect square number A such that N + A is also a perfect square number
- Smallest and Largest N-digit perfect cubes
- Number of perfect squares between two given numbers
- Count number less than N which are product of perfect squares
- Sort perfect squares in an array at their relative positions
- Check if the sum of perfect squares in an array is divisible by x
- Sum of distances between the two nearest perfect squares to all the nodes of the given linked list
- Print all perfect squares from the given range
- Sum of all Perfect Squares lying in the range [L, R] for Q queries
- Count of perfect squares of given length
- Count elements in an Array that can be represented as difference of two perfect squares
- Subsets of size K with product equal to difference of two perfect squares
- Count of primes in a given range that can be expressed as sum of perfect squares
- Count numbers upto N which are both perfect square and perfect cube
- Check if a number is a perfect square having all its digits as a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Largest number that is not a perfect square
- Largest factor of a given number which is a perfect square
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.