# Smallest number whose square has N digits

Given a number **N**, the task is to find the smallest number whose square has **N** digits.

**Examples:**

Input:N = 2

Output:4

Explanation:

3^{2}= 9, which has 1 digit.

4^{2}= 16, which has 2 digits.

Hence, 4 is the smallest number whose square has N digits.

Input:N = 3

Output:10

Explanation:

10^{2}= 100, which has 3 digits.

**Naive Approach:** The simplest approach to solve the problem is to calculate the square of each number starting from and count the number of digits in its square. Print the first number whose square is obtained to be of N digits.

**Time Complexity:** O(√(10^{N}))

**Efficent Approach:** To solve the problem, we need to make the following observations:

The smallest number whose square has 1 digit = 1

The smallest number whose square has 2 digits = 4

The smallest number whose square has 3 digits = 10

The smallest number whose square has 4 digits = 32

The smallest number whose square has 5 digits = 100Hence, these numbers form a series 1, 4, 10, 32, 100, 317, …….

Now, we need to find a formula for the **N ^{th} term** of the series.

The terms of the series can be expressed in the following form:

If

N = 1, Smallest number possible is 1.

If

N = 2, Smallest number possible is 41.

If

N = 3, Smallest number possible is 10.

Hence, we can conclude that the N^{th} of the series can be expressed as

Hence, in order to solve the problem, we just need to calculate ** ceil(10^{(N – 1)/ 2})** for the given integer

**N**.

Below is the implementation of the above approach:

## C++

`// C++ Program to find the smallest ` `// number whose square has N digits ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return smallest number ` `// whose square has N digits ` `int` `smallestNum(` `int` `N) ` `{ ` ` ` ` ` `// Calculate N-th term of the series ` ` ` `float` `x = ` `pow` `(10.0, (N - 1) / 2.0); ` ` ` `return` `ceil` `(x); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 4; ` ` ` `cout << smallestNum(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for above approach ` `class` `GFG{ ` ` ` `// Function to return smallest number ` `// whose square has N digits ` `static` `int` `smallestNum(` `int` `N) ` `{ ` ` ` ` ` `// Calculate N-th term of the series ` ` ` `float` `x = (` `float` `)(Math.pow(` `10` `, (N - ` `1` `) / ` `2.0` `)); ` ` ` `return` `(` `int` `)(Math.ceil(x)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `4` `; ` ` ` `System.out.print(smallestNum(N)); ` `} ` `} ` ` ` `// This code is contributed by spp ` |

*chevron_right*

*filter_none*

## C#

`// C# program for above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to return smallest number ` `// whose square has N digits ` `static` `int` `smallestNum(` `int` `N) ` `{ ` ` ` ` ` `// Calculate N-th term of the series ` ` ` `float` `x = (` `float` `)(Math.Pow(10, (N - 1) / 2.0)); ` ` ` `return` `(` `int` `)(Math.Ceiling(x)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 4; ` ` ` `Console.Write(smallestNum(N)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

32

**Time Complexity:** O(log(N))

**Auxiliary Space:** O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Smallest N digit number whose sum of square of digits is a Perfect Square
- Smallest number with given sum of digits and sum of square of digits
- Smallest number whose sum of digits is square of N
- Find smallest number with given number of digits and sum of digits under given constraints
- Sum of the digits of square of the given number which has only 1's as its digits
- Find smallest number with given number of digits and sum of digits
- Find smallest perfect square number A such that N + A is also a perfect square number
- Smallest even digits number not less than N
- Smallest odd digits number not less than N
- Smallest Even number with N digits
- Smallest odd number with N digits
- Find smallest possible Number from a given large Number with same count of digits
- Find the kth smallest number with sum of digits as m
- Smallest number with at least n digits in factorial
- Find the smallest number whose sum of digits is N
- Smallest number with sum of digits as N and divisible by 10^N
- Find the smallest number whose digits multiply to a given number n
- Minimum digits to remove to make a number Perfect Square
- Smallest number greater than or equal to N using only digits 1 to K
- Smallest number k such that the product of digits of k is equal to 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 contribute@geeksforgeeks.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.