📜  使用加法/减法检查完美平方

📅  最后修改于: 2021-04-24 19:22:07             🧑  作者: Mango

给定一个正整数n,仅使用加/减运算并以最小的时间复杂度检查它是否为完美平方。
强烈建议您最小化浏览器,然后自己尝试。
为此,我们可以使用奇数属性:

Addition of first n odd numbers is always perfect square 
1 + 3 = 4,      
1 + 3 + 5 = 9,     
1 + 3 + 5 + 7 + 9 + 11 = 36 ...

下面是上述想法的实现:

C++
// C++ program to check if n is perfect square
// or not
#include 
 
using namespace std;
 
// This function returns true if n is
// perfect square, else false
bool isPerfectSquare(int n)
{
    // sum is sum of all odd numbers. i is
    // used one by one hold odd numbers
    for (int sum = 0, i = 1; sum < n; i += 2) {
        sum += i;
        if (sum == n)
            return true;
    }
    return false;
}
 
// Driver code
int main()
{
    isPerfectSquare(35) ? cout << "Yes\n" : cout << "No\n";
    isPerfectSquare(49) ? cout << "Yes\n" : cout << "No\n";
    return 0;
}


Java
// Java program to check if n
// is perfect square or not
 
public class GFG {
 
    // This function returns true if n
    // is perfect square, else false
    static boolean isPerfectSquare(int n)
    {
        // sum is sum of all odd numbers. i is
        // used one by one hold odd numbers
        for (int sum = 0, i = 1; sum < n; i += 2) {
            sum += i;
            if (sum == n)
                return true;
        }
        return false;
    }
 
    // Driver Code
    public static void main(String args[])
    {
 
        if (isPerfectSquare(35))
            System.out.println("Yes");
        else
            System.out.println("NO");
 
        if (isPerfectSquare(49))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by Sam007


Python3
# This function returns true if n is
# perfect square, else false
def isPerfectSquare(n):
 
    # the_sum is sum of all odd numbers. i is
    # used one by one hold odd numbers
    i = 1
    the_sum = 0
    while the_sum < n:
        the_sum += i
        if the_sum == n:
            return True
        i += 2
    return False
 
# Driver code
if __name__ == "__main__":
    print('Yes') if isPerfectSquare(35) else print('NO')
    print('Yes') if isPerfectSquare(49) else print('NO')
 
# This code works only in Python 3


C#
// C# program to check if n
// is perfect square or not
using System;
 
public class GFG {
 
    // This function returns true if n
    // is perfect square, else false
    static bool isPerfectSquare(int n)
    {
        // sum is sum of all odd numbers. i is
        // used one by one hold odd numbers
        for (int sum = 0, i = 1; sum < n; i += 2) {
            sum += i;
            if (sum == n)
                return true;
        }
        return false;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
 
        if (isPerfectSquare(35))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
 
        if (isPerfectSquare(49))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


输出 :

No
Yes

时间复杂度: O(n)

辅助空间: O(1)
这是如何运作的?
以下是上述方法的说明。

1 + 3 + 5 + ...  (2n-1) = ∑(2*i - 1) where 1<=i<=n
                        = 2*∑i - ∑1  where 1<=i<=n
                        = 2n(n+1)/2 - n
                        = n(n+1) - n
                        = n2