# 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 = 2Output:4Explanation:

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 = 3Output:10Explanation:

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}))**Efficient 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 = 100

Hence, 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.

IfN = 2, Smallest number possible is 41.

IfN = 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;` `}` |

## 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` |

## Python3

`# Python3 Program to find the smallest` `# number whose square has N digits` `import` `math;` `# Function to return smallest number` `# whose square has N digits` `def` `smallestNum(N):` ` ` `# Calculate N-th term of the series` ` ` `x ` `=` `pow` `(` `10.0` `, (N ` `-` `1` `) ` `/` `2.0` `);` ` ` `return` `math.ceil(x);` `# Driver Code` `N ` `=` `4` `;` `print` `(smallestNum(N));` `# This code is contributed by Code_mech` |

## 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` |

## Javascript

`<script>` `// java script Program to find the smallest` `// number whose square has N digits` `// Function to return smallest number` `// whose square has N digits` `function` `smallestNum(N)` `{` ` ` `// Calculate N-th term of the series` ` ` `x = Math.pow(10.0, (N - 1) / 2.0);` ` ` `return` `Math.ceil(x);` `}` `// Driver Code` `let N = 4;` `document.write(smallestNum(N));` ` ` `// This code is contributed by Gottumukkala Bobby` `</script>` |

**Output:**

32

**Time Complexity:** O(log(N))**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**