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:
# Python3 implementation of the approach
# Function to print the largest and
# the smallest n-digit perfect squares
# Smallest n-digit perfect square
print(pow(math.ceil(math.sqrt(pow(10, n – 1))), 2),
end = ” “);
# Largest n-digit perfect square
print(pow(math.ceil(math.sqrt(pow(10, n))) – 1, 2));
# Driver code
n = 4;
# This code is contributed by mits
- Largest sub-array whose all elements are perfect squares
- Smallest and Largest N-digit perfect cubes
- Number of perfect squares between two given numbers
- Check if the sum of perfect squares in an array is divisible by x
- Count number less than N which are product of perfect squares
- Sort perfect squares in an array at their relative positions
- Largest number that is not a perfect square
- Smallest perfect Cube divisible by all elements of an array
- Smallest perfect square divisible by all elements of an array
- Largest factor of a given number which is a perfect square
- Largest number in an array that is not a perfect cube
- Largest perfect square number in an Array
- Largest Divisor of a Number not divisible by a perfect square
- Smallest and Largest Palindrome with N Digits
- Number of times the largest perfect square number can be subtracted from N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.