📜  计算给定范围内的Pronic编号

📅  最后修改于: 2021-04-17 17:17:41             🧑  作者: Mango

给定两个整数AB ,任务是计算范围[A,B]中存在的质子数。

例子:

天真的方法:按照给定的步骤解决问题:

  1. 将质子数计数初始化为0
  2. 对于[A,B]范围内的每个数字,检查是否是质子整数
  3. 如果发现为真,则增加计数。
  4. 最后,打印计数

下面是上述方法的实现:

C++14
// C++ program for
// the above approach
#include 
using namespace std;
 
// Function to check if x
// is a Pronic Number or not
bool checkPronic(int x)
{
 
    for (int i = 0; i <= (int)(sqrt(x));
         i++) {
 
        // Check for Pronic Number by
        // multiplying consecutive
        // numbers
        if (x == i * (i + 1)) {
            return true;
        }
    }
    return false;
}
 
// Function to count pronic
// numbers in the range [A, B]
void countPronic(int A, int B)
{
    // Initialise count
    int count = 0;
 
    // Iterate from A to B
    for (int i = A; i <= B; i++) {
 
        // If i is pronic
        if (checkPronic(i)) {
 
            // Increment count
            count++;
        }
    }
 
    // Print count
    cout << count;
}
 
// Driver Code
int main()
{
    int A = 3, B = 20;
 
    // Function call to count pronic
    // numbers in the range [A, B]
    countPronic(A, B);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to check if x
// is a Pronic Number or not
static boolean checkPronic(int x)
{
 
    for (int i = 0; i <= (int)(Math.sqrt(x));
         i++) {
 
        // Check for Pronic Number by
        // multiplying consecutive
        // numbers
        if ((x == i * (i + 1)) != false) {
            return true;
        }
    }
    return false;
}
 
// Function to count pronic
// numbers in the range [A, B]
static void countPronic(int A, int B)
{
    // Initialise count
    int count = 0;
 
    // Iterate from A to B
    for (int i = A; i <= B; i++) {
 
        // If i is pronic
        if (checkPronic(i) != false) {
 
            // Increment count
            count++;
        }
    }
 
    // Print count
    System.out.print(count);
}
 
// Driver Code
public static void main(String args[])
{
    int A = 3, B = 20;
 
    // Function call to count pronic
    // numbers in the range [A, B]
    countPronic(A, B);
}
}
 
// This code is contributed by sanjoy_62.


Python3
# Python3 program for the above approach
import math
 
# Function to check if x
# is a Pronic Number or not
def checkPronic(x) :
    for i in range(int(math.sqrt(x)) + 1):
 
        # Check for Pronic Number by
        # multiplying consecutive
        # numbers
        if (x == i * (i + 1)) :
            return True
    return False
   
# Function to count pronic
# numbers in the range [A, B]
def countPronic(A, B) :
     
    # Initialise count
    count = 0
 
    # Iterate from A to B
    for i in range(A, B + 1):
 
        # If i is pronic
        if (checkPronic(i) != 0) :
 
            # Increment count
            count += 1
         
    # Prcount
    print(count)
 
# Driver Code
A = 3
B = 20
 
# Function call to count pronic
# numbers in the range [A, B]
countPronic(A, B)
 
# This code is contributed by susmitakundugoaldanga.


C#
// C# program for the above approach
using System;
public class GFG
{
 
// Function to check if x
// is a Pronic Number or not
static bool checkPronic(int x)
{
 
    for (int i = 0; i <= (int)(Math.Sqrt(x));
         i++)
    {
 
        // Check for Pronic Number by
        // multiplying consecutive
        // numbers
        if ((x == i * (i + 1)) != false)
        {
            return true;
        }
    }
    return false;
}
 
// Function to count pronic
// numbers in the range [A, B]
static void countPronic(int A, int B)
{
   
    // Initialise count
    int count = 0;
 
    // Iterate from A to B
    for (int i = A; i <= B; i++)
    {
 
        // If i is pronic
        if (checkPronic(i) != false)
        {
 
            // Increment count
            count++;
        }
    }
 
    // Print count
    Console.Write(count);
}
 
 
// Driver Code
public static void Main(String[] args)
{
    int A = 3, B = 20;
 
    // Function call to count pronic
    // numbers in the range [A, B]
    countPronic(A, B);
}
}
 
// This code is contributed by code_hunt.


Javascript


C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count pronic
// numbers in the range [A, B]
int pronic(int num)
{
    // Check upto sqrt N
    int N = (int)sqrt(num);
 
    // If product of consecutive
    // numbers are less than equal to num
    if (N * (N + 1) <= num) {
        return N;
    }
 
    // Return N - 1
    return N - 1;
}
 
// Function to count pronic
// numbers in the range [A, B]
int countPronic(int A, int B)
{
    // Subtract the count of pronic numbers
    // which are <= (A - 1) from the count
    // f pronic numbers which are <= B
    return pronic(B) - pronic(A - 1);
}
 
// Driver Code
int main()
{
    int A = 3;
    int B = 20;
 
    // Function call to count pronic
    // numbers in the range [A, B]
    cout << countPronic(A, B);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to count pronic
// numbers in the range [A, B]
static int pronic(int num)
{
   
    // Check upto sqrt N
    int N = (int)Math.sqrt(num);
 
    // If product of consecutive
    // numbers are less than equal to num
    if (N * (N + 1) <= num)
    {
        return N;
    }
 
    // Return N - 1
    return N - 1;
}
 
// Function to count pronic
// numbers in the range [A, B]
static int countPronic(int A, int B)
{
   
    // Subtract the count of pronic numbers
    // which are <= (A - 1) from the count
    // f pronic numbers which are <= B
    return pronic(B) - pronic(A - 1);
}
 
// Driver Code
public static void main(String[] args)
{
    int A = 3;
    int B = 20;
 
    // Function call to count pronic
    // numbers in the range [A, B]
    System.out.print(countPronic(A, B));
 
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to count pronic
# numbers in the range [A, B]
def pronic(num) :
 
    # Check upto sqrt N
    N = int(num ** (1/2));
 
    # If product of consecutive
    # numbers are less than equal to num
    if (N * (N + 1) <= num) :
        return N;
 
    # Return N - 1
    return N - 1;
 
# Function to count pronic
# numbers in the range [A, B]
def countPronic(A, B) :
     
    # Subtract the count of pronic numbers
    # which are <= (A - 1) from the count
    # f pronic numbers which are <= B
    return pronic(B) - pronic(A - 1);
 
# Driver Code
if __name__ == "__main__" :
 
    A = 3;
    B = 20;
 
    # Function call to count pronic
    # numbers in the range [A, B]
    print(countPronic(A, B));
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
public class GFG
{
 
// Function to count pronic
// numbers in the range [A, B]
static int pronic(int num)
{
   
    // Check upto sqrt N
    int N = (int)Math.Sqrt(num);
 
    // If product of consecutive
    // numbers are less than equal to num
    if (N * (N + 1) <= num)
    {
        return N;
    }
 
    // Return N - 1
    return N - 1;
}
 
// Function to count pronic
// numbers in the range [A, B]
static int countPronic(int A, int B)
{
   
    // Subtract the count of pronic numbers
    // which are <= (A - 1) from the count
    // f pronic numbers which are <= B
    return pronic(B) - pronic(A - 1);
}
 
// Driver Code
public static void Main(String[] args)
{
    int A = 3;
    int B = 20;
 
    // Function call to count pronic
    // numbers in the range [A, B]
    Console.Write(countPronic(A, B));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3

时间复杂度: O((B – A)*√(B – A))
辅助空间: O(1)

高效的方法:请按照以下步骤解决问题:

  1. 定义一个函数pronic(N),以找到pronic整数其是≤n的计数。
  2. 计算N的平方根,例如X。
  3. 如果XX – 1的乘积小于或等于N ,则返回N。
  4. 否则,返回N – 1
  5. 打印pronic(B)– pronic(A – 1)以获得[A,B]范围内的pronic整数计数

下面是上述方法的实现:

C++ 14

// C++ program for the above approach
#include 
using namespace std;
 
// Function to count pronic
// numbers in the range [A, B]
int pronic(int num)
{
    // Check upto sqrt N
    int N = (int)sqrt(num);
 
    // If product of consecutive
    // numbers are less than equal to num
    if (N * (N + 1) <= num) {
        return N;
    }
 
    // Return N - 1
    return N - 1;
}
 
// Function to count pronic
// numbers in the range [A, B]
int countPronic(int A, int B)
{
    // Subtract the count of pronic numbers
    // which are <= (A - 1) from the count
    // f pronic numbers which are <= B
    return pronic(B) - pronic(A - 1);
}
 
// Driver Code
int main()
{
    int A = 3;
    int B = 20;
 
    // Function call to count pronic
    // numbers in the range [A, B]
    cout << countPronic(A, B);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to count pronic
// numbers in the range [A, B]
static int pronic(int num)
{
   
    // Check upto sqrt N
    int N = (int)Math.sqrt(num);
 
    // If product of consecutive
    // numbers are less than equal to num
    if (N * (N + 1) <= num)
    {
        return N;
    }
 
    // Return N - 1
    return N - 1;
}
 
// Function to count pronic
// numbers in the range [A, B]
static int countPronic(int A, int B)
{
   
    // Subtract the count of pronic numbers
    // which are <= (A - 1) from the count
    // f pronic numbers which are <= B
    return pronic(B) - pronic(A - 1);
}
 
// Driver Code
public static void main(String[] args)
{
    int A = 3;
    int B = 20;
 
    // Function call to count pronic
    // numbers in the range [A, B]
    System.out.print(countPronic(A, B));
 
}
}
 
// This code is contributed by shikhasingrajput

Python3

# Python3 program for the above approach
 
# Function to count pronic
# numbers in the range [A, B]
def pronic(num) :
 
    # Check upto sqrt N
    N = int(num ** (1/2));
 
    # If product of consecutive
    # numbers are less than equal to num
    if (N * (N + 1) <= num) :
        return N;
 
    # Return N - 1
    return N - 1;
 
# Function to count pronic
# numbers in the range [A, B]
def countPronic(A, B) :
     
    # Subtract the count of pronic numbers
    # which are <= (A - 1) from the count
    # f pronic numbers which are <= B
    return pronic(B) - pronic(A - 1);
 
# Driver Code
if __name__ == "__main__" :
 
    A = 3;
    B = 20;
 
    # Function call to count pronic
    # numbers in the range [A, B]
    print(countPronic(A, B));
 
    # This code is contributed by AnkThon

C#

// C# program for the above approach
using System;
public class GFG
{
 
// Function to count pronic
// numbers in the range [A, B]
static int pronic(int num)
{
   
    // Check upto sqrt N
    int N = (int)Math.Sqrt(num);
 
    // If product of consecutive
    // numbers are less than equal to num
    if (N * (N + 1) <= num)
    {
        return N;
    }
 
    // Return N - 1
    return N - 1;
}
 
// Function to count pronic
// numbers in the range [A, B]
static int countPronic(int A, int B)
{
   
    // Subtract the count of pronic numbers
    // which are <= (A - 1) from the count
    // f pronic numbers which are <= B
    return pronic(B) - pronic(A - 1);
}
 
// Driver Code
public static void Main(String[] args)
{
    int A = 3;
    int B = 20;
 
    // Function call to count pronic
    // numbers in the range [A, B]
    Console.Write(countPronic(A, B));
}
}
 
// This code is contributed by 29AjayKumar

Java脚本


输出:
3

时间复杂度: O(log 2 (B))
辅助空间: O(1)