📌  相关文章
📜  查询以查找数字是否正好具有四个不同的因子

📅  最后修改于: 2021-04-27 06:10:44             🧑  作者: Mango

给定正整数“ q”和“ n”。对于每个查询“ q”,查找数字“ n”是否正好具有四个不同的除数。如果数字正好有四个除数,则打印“是”,否则打印“否”。1<= q,n <= 10 6

Input:
2
10
12
Output:
Yes
No

Explanation:
For 1st query, n = 10 has exactly four divisor i.e., 1, 2, 5, 10.
For 2nd query, n = 12 has exactly six divisor i.e., 1, 2, 3, 4, 6, 12.

一种简单的方法是通过使用此方法生成一个数的所有除数来对因子进行计数,然后检查所有因子的计数是否等于“ 4”。此方法的时间复杂度为O(sqrt(n))。

更好的方法是使用数论。为了使一个数具有四个因素,它必须满足以下条件:

  1. 如果数字是正好是两个质数的乘积(例如p,q)。因此,我们可以确保它将具有四个因子,即1,p,q,n。
  2. 如果一个数字是质数的立方(或该数字的立方根是质数)。例如,假设n = 8,立方根= 2,这意味着’8’可以写为2 * 2 * 2,因此有四个因数:-1、2、4和8。

我们可以使用Eratosthenes筛子,这样我们可以预先计算从1到10 6的所有素数。现在,我们将通过使用两个“ for循环”(即mark [p * q] = true)来标记所有是两个质数的乘积的数字。同时,我们还将通过采用数字立方来标记所有数字(立方根),即mark [p * p * p] = true。
之后,我们可以轻松地在O(1)时间内回答每个查询。
下面是伪代码,请看一下以便更好的理解

C++
// C++ program to check whether number has
// exactly four distinct factors or not
#include 
using namespace std;
  
// Initialize global variable according
// to given condition so that it can be
// accessible to all function
const int N = 1e6;
bool fourDiv[N + 1];
  
// Function to calculate all number having
// four distinct distinct factors
void fourDistinctFactors()
{
    // Create a boolean array "prime[0..n]" and 
    // initialize all entries it as true. A value 
    // in prime[i] will finally be false if i is 
    // not a prime, else true.
    bool primeAll[N + 1];
    memset(primeAll, true, sizeof(primeAll));
  
    for (int p = 2; p * p <= N; p++) {
  
        // If prime[p] is not changed, then it
        // is a prime
        if (primeAll[p] == true) {
  
            // Update all multiples of p
            for (int i = p * 2; i <= N; i += p)
                primeAll[i] = false;
        }
    }
  
    // Initialize prime[] array which will
    // contains all the primes from 1-N
    vector prime;
  
    for (int p = 2; p <= N; p++)
        if (primeAll[p])
            prime.push_back(p);
  
    // Set the marking of all primes to false
    memset(fourDiv, false, sizeof(fourDiv));
  
    // Iterate over all the prime numbers
    for (int i = 0; i < prime.size(); ++i) {
        int p = prime[i];
  
        // Mark cube root of prime numbers
        if (1LL * p * p * p <= N)
            fourDiv[p * p * p] = true;
  
        for (int j = i + 1; j < prime.size(); ++j) {
            int q = prime[j];
  
            if (1LL * p * q > N)
                break;
  
            // Mark product of prime numbers
            fourDiv[p * q] = true;
        }
    }
}
  
// Driver program
int main()
{
    fourDistinctFactors();
  
    int num = 10;
    if (fourDiv[num])
        cout << "Yes\n";
    else
        cout << "No\n";
  
    num = 12;
    if (fourDiv[num])
        cout << "Yes\n";
    else
        cout << "No\n";
  
    return 0;
}


Java
// Java program to check whether number has
// exactly four distinct factors or not
import java.util.*;
class GFG{
// Initialize global variable according
// to given condition so that it can be
// accessible to all function
static int N = (int)1E6;
static boolean[] fourDiv=new boolean[N + 1];
  
// Function to calculate all number having
// four distinct distinct factors
static void fourDistinctFactors()
{
    // Create a boolean array "prime[0..n]" and 
    // initialize all entries it as true. A value 
    // in prime[i] will finally be false if i is 
    // not a prime, else true.
    boolean[] primeAll=new boolean[N + 1];
  
    for (int p = 2; p * p <= N; p++) {
  
        // If prime[p] is not changed, then it
        // is a prime
        if (primeAll[p] == false) {
  
            // Update all multiples of p
            for (int i = p * 2; i <= N; i += p)
                primeAll[i] = true;
        }
    }
  
    // Initialize prime[] array which will
    // contains all the primes from 1-N
    ArrayList prime=new ArrayList();
  
    for (int p = 2; p <= N; p++)
        if (!primeAll[p])
            prime.add(p);
  
  
    // Iterate over all the prime numbers
    for (int i = 0; i < prime.size(); ++i) {
        int p = prime.get(i);
  
        // Mark cube root of prime numbers
        if (1L * p * p * p <= N)
            fourDiv[p * p * p] = true;
  
        for (int j = i + 1; j < prime.size(); ++j) {
            int q = prime.get(j);
  
            if (1L * p * q > N)
                break;
  
            // Mark product of prime numbers
            fourDiv[p * q] = true;
        }
    }
}
  
// Driver program
public static void main(String[] args)
{
    fourDistinctFactors();
  
    int num = 10;
    if (fourDiv[num])
        System.out.println("Yes");
    else
        System.out.println("No");
  
    num = 12;
    if (fourDiv[num])
        System.out.println("Yes");
    else
        System.out.println("No");
  
}
}
// This code is contributed by mits


Python3
# Python3 program to check whether number 
# has exactly four distinct factors or not
  
# Initialize global variable according to 
# given condition so that it can be 
# accessible to all function
N = 1000001;
fourDiv = [False] * (N + 1);
  
# Function to calculate all number 
# having four distinct factors
def fourDistinctFactors():
      
    # Create a boolean array "prime[0..n]" 
    # and initialize all entries it as true. 
    # A value in prime[i] will finally be 
    # false if i is not a prime, else true.
    primeAll = [True] * (N + 1);
    p = 2;
  
    while (p * p <= N): 
  
        # If prime[p] is not changed, then it 
        # is a prime
        if (primeAll[p] == True):
  
            # Update all multiples of p
            i = p * 2;
            while (i <= N):
                primeAll[i] = False;
                i += p;
        p += 1;
  
    # Initialize prime[] array which will 
    # contain all the primes from 1-N
    prime = [];
  
    for p in range(2, N + 1):
        if (primeAll[p]):
            prime.append(p);
  
    # Iterate over all the prime numbers
    for i in range(len(prime)): 
        p = prime[i];
  
        # Mark cube root of prime numbers
        if (1 * p * p * p <= N):
            fourDiv[p * p * p] = True;
  
        for j in range(i + 1, len(prime)):
            q = prime[j];
  
            if (1 * p * q > N):
                break;
  
            # Mark product of prime numbers
            fourDiv[p * q] = True;
  
# Driver Code
fourDistinctFactors();
  
num = 10;
if (fourDiv[num]):
    print("Yes");
else:
    print("No");
  
num = 12;
if (fourDiv[num]):
    print("Yes");
else:
    print("No");
  
# This code is contributed by mits


C#
// C# program to check whether number has
// exactly four distinct factors or not
using System;
using System.Collections;
  
class GFG
{
      
// Initialize global variable according
// to given condition so that it can be
// accessible to all function
static int N = (int)1E6;
static bool[] fourDiv = new bool[N + 1];
  
// Function to calculate all number having
// four distinct distinct factors
static void fourDistinctFactors()
{
    // Create a boolean array "prime[0..n]" and 
    // initialize all entries it as true. A value 
    // in prime[i] will finally be false if i is 
    // not a prime, else true.
    bool[] primeAll = new bool[N + 1];
  
    for (int p = 2; p * p <= N; p++) 
    {
  
        // If prime[p] is not changed, 
        // then it is a prime
        if (primeAll[p] == false) 
        {
  
            // Update all multiples of p
            for (int i = p * 2; i <= N; i += p)
                primeAll[i] = true;
        }
    }
  
    // Initialize prime[] array which will
    // contains all the primes from 1-N
    ArrayList prime = new ArrayList();
  
    for (int p = 2; p <= N; p++)
        if (!primeAll[p])
            prime.Add(p);
  
    // Iterate over all the prime numbers
    for (int i = 0; i < prime.Count; ++i)
    {
        int p = (int)prime[i];
  
        // Mark cube root of prime numbers
        if (1L * p * p * p <= N)
            fourDiv[p * p * p] = true;
  
        for (int j = i + 1; j < prime.Count; ++j) 
        {
            int q = (int)prime[j];
  
            if (1L * p * q > N)
                break;
  
            // Mark product of prime numbers
            fourDiv[p * q] = true;
        }
    }
}
  
// Driver Code
public static void Main()
{
    fourDistinctFactors();
  
    int num = 10;
    if (fourDiv[num])
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
  
    num = 12;
    if (fourDiv[num])
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by mits


PHP
 $N)
                break;
  
            // Mark product of 
            // prime numbers
            $fourDiv[$p * $q] = true;
        }
    }
}
  
// Driver Code
fourDistinctFactors();
  
$num = 10;
if ($fourDiv[$num])
    echo "Yes\n";
else
    echo "No\n";
  
$num = 12;
if ($fourDiv[$num])
    echo "Yes\n";
else
    echo "No\n";
  
// This code is contributed by mits
?>


输出:

Yes
No


时间复杂度:
每个查询为O(1)。