📜  检查给定的号码是否为Pronic |高效方法

📅  最后修改于: 2021-04-24 05:09:31             🧑  作者: Mango

质子数是可以表示为两个连续正整数的乘积的数。通过将这两个连续的正整数相乘,可以形成一个由乘积或质子数表示的矩形。因此,它也被称为矩形数。
前几个Pronic编号是:
0、2、6、12、20、30、42、56、72、90、110、132、156、182、210、240、272、306、342、380、420、462。 。 。 。 。 。
质子数是两个连续整数的乘积,即数n是x和(x + 1)的积。任务是检查给定的数字是否为质子。
数学表示法:

If x is a pronic number, then x=n(n+1) ∀ n∈N0
Where, N0={0, 1, 2, 3, 4, ....}, (A set of Naturral Numbers)

例子:

Input : 56
Output : YES
Explanation: 56 = 7 * 8 i.e 56 is a product 
of two consecutive integers 7 and 8.

Input : 65
Output : NO
Explanation: 65 cannot be represented as a
product of any two consecutive integers.

前面我们已经使用循环讨论了一种检查数字是否为质数的方法。先前算法的时间复杂度相对较高,并且按照Big-O渐近符号表示为O(√n)。
在本文中,我们将解释一种有效的方法,其时间复杂度为O(log(log n)。更精确的观察将导致这样一个事实,即只有当floor(sqrt(N))和floor(sqrt(N)的乘积时,数字N才能表示为两个连续整数的乘积)+1等于N
下面是上述方法的逐步算法:

Step 1: Evaluate the square root value of the given number.
Step 2: Calculate the floor value of that square root.
Step 3: Calculate the product of value calculated in step-2
    and its next consecutive number.
Step 4: Check the product value in step-3 with the given number.
    Step 4.1: If the condition satisfies,
          then the number is a pronic number.
    Step 4.2: Otherwise the number is not a pronic number.

下面是上述算法的实现:

C
// C/C++ program to check if a number is pronic or not
 
#include
using namespace std;
 
// function to check Pronic Number
bool pronic_check(int n)
{
    int x = (int)(sqrt(n));
 
    // Checking Pronic Number by
    // multiplying consecutive numbers
    if (x*(x+1)==n)
        return true;
    else
        return false;
}
 
// Driver Code
int main(void)
{
    int n = 56;   
    pronic_check(n) == true? cout << "YES" :
                             cout << "NO";
     
    return 0;
}


Java
// Java program to check if a number is pronic or not
 
import java.io.*;
import java.util.*;
import java.math.*;
 
class GFG
{
 
    // Function to check Pronic Number
    static boolean pronic_check(int n)
    {
        int x = (int)(Math.sqrt(n));
     
        // Checking Pronic Number by
        // multiplying consecutive numbers
        if (x * (x + 1) == n)
            return true;
        else
            return false;
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        int n = 56;       
        if (pronic_check(n)==true)
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}


Python3
# Python program to check if a number is pronic or not
 
import math
 
# function to check Pronic Number
def pronic_check(n) :
    x = (int)(math.sqrt(n))
 
    # Checking Pronic Number by multiplying
    # consecutive numbers
    if (x*(x + 1)== n):
        return True
    else:
        return False
 
# Driver Code
n = 56
 
if (pronic_check(n)==True):
    print("YES")
else:
    print("NO")


C#
// C# program to check if a number is
// pronic or not
using System;
 
class GFG
{
 
    // Function to check Pronic Number
    static bool pronic_check(int n)
    {
        int x = (int)(Math.Sqrt(n));
     
        // Checking Pronic Number by
        // multiplying consecutive numbers
        if (x * (x + 1) == n)
            return true;
        else
            return false;
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 56;
         
        if (pronic_check(n)==true)
            Console.Write("YES");
        else
            Console.Write("NO");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

YES