📜  百隆整数

📅  最后修改于: 2021-05-04 14:40:06             🧑  作者: Mango

Blum Integer是一个半素数,假设p和q是两个因子(即n = p * q),它们(p和q)的形式为4t + 3,其中t是某个整数。
前几个Blum Integer是21、33、57、69、77、93、129、133、141、161、177,…
注意:由于两个因子都应为半素数,因此偶数不能是Blum整数,也不能是20以下的数字,
因此,无论是否为Blum Integer,我们都只需检查大于20的奇数整数。

例子 :

Input: 33
Output: Yes
Explanation: 33 = 3 * 11, 3 and 11 are both 
semi-primes as well as of the form 4t + 3 
for t = 0, 2

Input: 77
Output:  Yes
Explanation: 77 = 7 * 11, 7 and 11 both are 
semi-prime as well as of the form 4t + 3 
for t = 1, 2

Input: 25
Output: No
Explanation: 25 = 5*5, 5 and 5 are both 
semi-prime  but are not of the form 4t 
+ 3, Hence 25 is not a Blum integer.

方法:对于给定的大于20的奇整数,我们计算从1到该奇整数的质数。如果我们找到任何将那个奇数整数除以它的商均是素数的质数,并且对于某个整数,其形式为4t + 3,那么给定的奇数整数就是Blum Integer。

下面是上述方法的实现:

C++
// CPP program to check if a number is a Blum
// integer
#include 
using namespace std;
 
// Function to cheek if number is Blum Integer
bool isBlumInteger(int n)
{
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
 
    // to store prime numbers from 2 to n
    for (int i = 2; i * i <= n; i++) {
 
        // If prime[i] is not
        // changed, then it is a prime
        if (prime[i] == true) {
 
            // Update all multiples of p
            for (int j = i * 2; j <= n; j += i)
                prime[j] = false;
        }
    }
 
    // to check if the given odd integer
    // is Blum Integer or not
    for (int i = 2; i <= n; i++) {
        if (prime[i]) {
 
            // checking the factors
            // are of 4t+3 form or not
            if ((n % i == 0) && ((i - 3) % 4) == 0) {
                int q = n / i;
                return (q != i && prime[q] &&
                           (q - 3) % 4 == 0);
            }
        }
    }
 
    return false;
}
 
// driver code
int main()
{
    // give odd integer greater than 20
    int n = 249;
 
    if (isBlumInteger(n))
        cout << "Yes";
    else
        cout << "No";
}


Java
// Java implementation to check If
// a number is a Blum integer
import java.util.*;
class GFG {
    public static boolean isBlumInteger(int n)
    {
        boolean prime[] = new boolean[n + 1];
        for (int i = 0; i < n; i++)
            prime[i] = true;
 
        // to store prime numbers from 2 to n
        for (int i = 2; i * i <= n; i++) {
 
            // If prime[i] is not changed,
            // then it is a prime
            if (prime[i] == true) {
 
                // Update all multiples of p
                for (int j = i * 2; j <= n; j += i)
                    prime[j] = false;
            }
        }
 
        // to check if the given odd integer
        // is Blum Integer or not
        for (int i = 2; i <= n; i++) {
            if (prime[i]) {
 
                // checking the factors are
                // of 4t + 3 form or not
                if ((n % i == 0) && ((i - 3) % 4) == 0) {
                    int q = n / i;
                    return (q != i && prime[q] &&
                            (q - 3) % 4 == 0);
                }
            }
        }
        return false;
    }
 
    // driver code
    public static void main(String[] args)
    {
        // give odd integer greater than 20
        int n = 249;
 
        if (isBlumInteger(n) == true)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


Python 3
# python 3 program to check if a
# number is a Blum integer
 
# Function to cheek if number
# is Blum Integer
def isBlumInteger(n):
 
    prime = [True]*(n + 1)
 
    # to store prime numbers from 2 to n
    i = 2
    while (i * i <= n):
 
        # If prime[i] is not
        # changed, then it is a prime
        if (prime[i] == True) :
 
            # Update all multiples of p
            for j in range(i * 2, n + 1, i):
                prime[j] = False
        i = i + 1
     
    # to check if the given odd integer
    # is Blum Integer or not
    for i in range(2, n + 1) :
        if (prime[i]) :
 
            # checking the factors
            # are of 4t+3 form or not
            if ((n % i == 0) and
                        ((i - 3) % 4) == 0):
                q = int(n / i)
                return (q != i and prime[q]
                       and (q - 3) % 4 == 0)
             
    return False
 
# driver code
# give odd integer greater than 20
n = 249
if (isBlumInteger(n)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Smitha.


C#
// C# implementation to check If
// a number is a Blum integer
using System;
 
class GFG {
     
    public static bool isBlumInteger(int n)
    {
        bool[] prime = new bool[n + 1];
        for (int i = 0; i < n; i++)
            prime[i] = true;
 
        // to store prime numbers from 2 to n
        for (int i = 2; i * i <= n; i++) {
 
            // If prime[i] is not changed,
            // then it is a prime
            if (prime[i] == true) {
 
                // Update all multiples of p
                for (int j = i * 2; j <= n; j += i)
                    prime[j] = false;
            }
        }
 
        // to check if the given odd integer
        // is Blum Integer or not
        for (int i = 2; i <= n; i++) {
            if (prime[i]) {
 
                // checking the factors are
                // of 4t + 3 form or not
                if ((n % i == 0) && ((i - 3) % 4) == 0)
                {
                    int q = n / i;
                    return (q != i && prime[q] &&
                           (q - 3) % 4 == 0);
                }
            }
        }
        return false;
    }
     
    // Driver code
    static public void Main ()
    {
        // give odd integer greater than 20
        int n = 249;
 
        if (isBlumInteger(n) == true)
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by Ajit.


PHP


输出:
Yes