Given n, find the nth number which is not a perfect square among natural numbers (1, 2, 3, 4, 5, 6, … )
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.
The 16th Non-Square number is 20
- Number of digits in the nth number made of given four digits
- Find Nth number in a sequence which is not a multiple of a given number
- Find nth Magic Number
- Nth Even Fibonacci Number
- Nth Square free number
- Program to find last two digits of Nth Fibonacci number
- Find nth Fibonacci number using Golden ratio
- Find nth number that contains the digit k or divisible by k.
- Find Nth even length palindromic number formed using digits X and Y
- Find the Nth Mosaic number
- Program to find the nth Kynea number
- Find Nth number of the series 1, 6, 15, 28, 45, .....
- Program to find the Nth Harmonic Number
- Program to find the Nth number of the series 2, 10, 24, 44, 70.....
- Nth number made up of odd digits only
- Find nth Hermite number
- New Algorithm to Generate Prime Numbers from 1 to Nth Number
- Program to delete Nth digit of a Number
- Find Nth positive number whose digital root is X
- Nth number whose sum of digit is multiple of 10
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.