📜  第N个非平方数

📅  最后修改于: 2021-04-26 18:53:22             🧑  作者: Mango

给定n ,找到自然数(1、2、3、4、5、6…)中不是完美平方的第n个数

例子:

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

Input : 5
Output : 7

Input : 16
Output : 20

查看问题陈述,我们可以得出一种简单明了的暴力方法。我们可以从n = 1开始,然后开始检查它们是否是一个完美的正方形。因此,我们可以得出第n个非平方数。

但是,上述方法非常慢,因为它每次都搜索小于目标的每个数字。

我们可以看到正在考虑的序列是2、3、5、6、7、8、10、11、12、13、14、15、17…。
通过检查,我们可以得出此序列中第n个数字的恒定时间公式。
ans(n) = n + \left \lfloor \frac{1}{2} + \sqrt{n}\right \rfloor

公式的正确性可以通过数学归纳原理来证明。

上面公式的实现如下。

C++
// CPP program to find n-th non-square number.
#include 
  
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;
}


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));
}
}


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.


PHP


输出:

The 16th Non-Square number is 20

时间复杂度\mathcal{O}(1)
空间复杂度\mathcal{O}(1)