# Nth non-Square number

Given **n**, find the **nth** number which is not a perfect square among natural numbers (1, 2, 3, 4, 5, 6, … )

**Examples:**

Input : 3 Output : 5 First three non-square numbers are 2, 3 and 5 Input : 5 Output : 7 Input : 16 Output : 20

Looking at the problem statement we can come up to a straight-forward brute-force approach. We can start from n = 1, and start to check if each of them is a perfect square or not. So we can come up to the nth non-square number.

However, the above approach is very slow as it searches for each in every number smaller than the target each time.

We can observe that the series under consideration is 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, … .

We can come upto the constant time formula for the nth number in this sequence, by inspection.

.

The correctness of the formula can be proved by the Principle of Mathematical Induction.

The implementation of the above formula is given below.

## C++

`// CPP program to find n-th non-square number. ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// function to find the nth Non-Square Number ` `int` `findNthNonSquare(` `int` `n) ` `{ ` ` ` `// conversion from int to long double is ` ` ` `// necessary in order to preserve decimal ` ` ` `// places after square root. ` ` ` `long` `double` `x = (` `long` `double` `)n; ` ` ` ` ` `// calculating the result ` ` ` `long` `double` `ans = x + ` `floor` `(0.5 + ` `sqrt` `(x)); ` ` ` ` ` `return` `(` `int` `)ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// initializing the term number ` ` ` `int` `n = 16; ` ` ` ` ` `// Print the result ` ` ` `cout << ` `"The "` `<< n << ` `"th Non-Square number is "` `; ` ` ` `cout << findNthNonSquare(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find ` `// n-th non-square number. ` `import` `java.io.*; ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `class` `GFG ` `{ ` ` ` `// function to find the ` `// nth Non-Square Number ` `static` `int` `findNthNonSquare(` `int` `n) ` `{ ` ` ` `// conversion from int to ` ` ` `// long double is necessary ` ` ` `// in order to preserve decimal ` ` ` `// places after square root. ` ` ` `double` `x = (` `double` `)n; ` ` ` ` ` `// calculating the result ` ` ` `double` `ans = x + Math.floor(` `0.5` `+ ` ` ` `Math.sqrt(x)); ` ` ` ` ` `return` `(` `int` `)ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// initializing ` ` ` `// the term number ` ` ` `int` `n = ` `16` `; ` ` ` ` ` `// Print the result ` ` ` `System.out.print(` `"The "` `+ n + ` ` ` `"th Non-Square number is "` `); ` ` ` `System.out.print(findNthNonSquare(n)); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to find n-th

# non-square number.

import math

# function to find the nth

# Non-Square Number

def findNthNonSquare(n):

# conversion from int to long

# double is necessary in order

# to preserve decimal places

# after square root.

x = n;

# calculating the result

ans = x + math.floor(0.5 + math.sqrt(x));

return int(ans);

# Driver code

# initializing the term number

n = 16;

# Print the result

print(“The”, n, “th Non-Square number is”,

findNthNonSquare(n));

# This code is contributed by mits

## C#

`// C# program to find ` `// n-th non-square number. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// function to find the ` `// nth Non-Square Number ` `static` `int` `findNthNonSquare(` `int` `n) ` `{ ` ` ` `// conversion from int ` ` ` `// to long double is ` ` ` `// necessary in order ` ` ` `// to preserve decimal ` ` ` `// places after square ` ` ` `// root. ` ` ` `double` `x = (` `double` `)n; ` ` ` ` ` `// calculating the result ` ` ` `double` `ans = x + Math.Floor(0.5 + ` ` ` `Math.Sqrt(x)); ` ` ` ` ` `return` `(` `int` `)ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `// initializing ` ` ` `// the term number ` ` ` `int` `n = 16; ` ` ` ` ` `// Print the result ` ` ` `Console.Write(` `"The "` `+ n + ` ` ` `"th Non-Square "` `+ ` ` ` `"number is "` `); ` ` ` `Console.Write(findNthNonSquare(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find n-th ` `// non-square number. ` ` ` `// function to find the nth ` `// Non-Square Number ` `function` `findNthNonSquare(` `$n` `) ` `{ ` ` ` `// conversion from int to long ` ` ` `// double is necessary in order ` ` ` `// to preserve decimal places ` ` ` `// after square root. ` ` ` `$x` `= ` `$n` `; ` ` ` ` ` `// calculating the result ` ` ` `$ans` `= ` `$x` `+ ` `floor` `(0.5 + sqrt(` `$x` `)); ` ` ` ` ` `return` `(int)` `$ans` `; ` `} ` ` ` `// Driver code ` ` ` `// initializing the term number ` `$n` `= 16; ` ` ` `// Print the result ` `echo` `"The "` `. ` `$n` `. ` ` ` `"th Non-Square number is "` `; ` `echo` `findNthNonSquare(` `$n` `); ` ` ` `// This Code is Contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

The 16th Non-Square number is 20

**Time Complexity** .

**Space Complexity**

## Recommended Posts:

- Count number of trailing zeros in Binary representation of a number using Bitset
- Find minimum number to be divided to make a number a perfect square
- Count number of triplets with product equal to given number with duplicates allowed
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Number of times the largest perfect square number can be subtracted from N
- Total number of possible Binary Search Trees using Catalan Number
- Maximum number of contiguous array elements with same number of set bits
- Given number of matches played, find number of teams in tournament
- Count the number of operations required to reduce the given number
- Querying maximum number of divisors that a number in a given range has
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Find the smallest number whose digits multiply to a given number n
- Find the number of jumps to reach X in the number line from zero
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Build Lowest Number by Removing n digits from a given number

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.