📌  相关文章
📜  计算给定范围内恰好有 5 个不同因子的数字

📅  最后修改于: 2021-10-26 05:30:43             🧑  作者: Mango

给定两个整数LR ,任务是计算范围[L, R]中恰好有5 个不同正因子的数字的计数

例子:

朴素的方法:解决这个问题最简单的方法是遍历范围[L, R]并为每个数字计算其因子。如果因子数等于5 ,则将 count 增加1
时间复杂度: (R – L) × √N
辅助空间: O(1)
有效的方法:为了优化上述方法,需要对恰好具有 5 个因子的数字进行以下观察。

请按照以下步骤解决问题:

  • 所需的计数是包含 p 4作为因子的范围内的数字计数,其中p是素数。
  • 为了有效地计算大范围 ( [1, 10 18 ] ) 的 p 4 ,其想法是使用埃拉托色尼筛来存储最大为10 4.5 的所有素数

下面是上述方法的实现:

C++14
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
const int N = 2e5;
 
// Stores all prime numbers
// up to 2 * 10^5
vector prime;
 
// Function to generate all prime
// numbers up to 2 * 10 ^ 5 using
// Sieve of Eratosthenes
void Sieve()
{
    prime.clear();
    vector p(N + 1, true);
 
    // Mark 0 and 1 as non-prime
    p[0] = p[1] = false;
 
    for (int i = 2; i * i <= N; i++) {
 
        // If i is prime
        if (p[i] == true) {
 
            // Mark all its factors as non-prime
            for (int j = i * i; j <= N; j += i) {
                p[j] = false;
            }
        }
    }
 
    for (int i = 1; i < N; i++) {
 
        // If current number is prime
        if (p[i]) {
 
            // Store the prime
            prime.push_back(1LL * pow(i, 4));
        }
    }
}
 
// Function to count numbers in the
// range [L, R] having exactly 5 factors
void countNumbers(long long int L,
                  long long int R)
{
 
    // Stores the required count
    int Count = 0;
 
    for (int p : prime) {
 
        if (p >= L && p <= R) {
            Count++;
        }
    }
    cout << Count << endl;
}
 
// Driver Code
int main()
{
    long long L = 16, R = 85000;
 
    Sieve();
 
    countNumbers(L, R);
 
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG
{
  static int N = 200000;
 
  // Stores all prime numbers
  // up to 2 * 10^5
  static int prime[] = new int [20000];
  static int index = 0;
 
  // Function to generate all prime
  // numbers up to 2 * 10 ^ 5 using
  // Sieve of Eratosthenes
  static void Sieve()
  {
    index = 0;
    int p[] = new int [N + 1];
    for(int i = 0; i <= N; i++)
    {
      p[i] = 1;
    }
 
    // Mark 0 and 1 as non-prime
    p[0] = p[1] = 0;
    for (int i = 2; i * i <= N; i++)
    {
 
      // If i is prime
      if (p[i] == 1)
      {
 
        // Mark all its factors as non-prime
        for (int j = i * i; j <= N; j += i)
        {
          p[j] = 0;
        }
      }
    }
    for (int i = 1; i < N; i++)
    {
 
      // If current number is prime
      if (p[i] == 1)
      {
 
        // Store the prime
        prime[index++] = (int)(Math.pow(i, 4));
      }
    }
  }
 
  // Function to count numbers in the
  // range [L, R] having exactly 5 factors
  static void countNumbers(int L,int R)
  {
 
    // Stores the required count
    int Count = 0;
    for(int i = 0; i < index; i++)
    {
      int p = prime[i];
      if (p >= L && p <= R)
      {
        Count++;
      }
    }
    System.out.println(Count);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int L = 16, R = 85000;
    Sieve();
    countNumbers(L, R);
  }
}
 
// This code is contributed by amreshkumar3.


Python3
# Python3 implementation of
# the above approach
N = 2 * 100000
 
# Stores all prime numbers
# up to 2 * 10^5
prime = [0] * N
 
# Function to generate all prime
# numbers up to 2 * 10 ^ 5 using
# Sieve of Eratosthenes
def Sieve() :
    p = [True] * (N + 1)
 
    # Mark 0 and 1 as non-prime
    p[0] = p[1] = False
    i = 2
    while(i * i <= N) :
 
        # If i is prime
        if (p[i] == True) :
 
            # Mark all its factors as non-prime
            for j in range(i * i, N, i):
                p[j] = False    
        i += 1
    for i in range(N):
 
        # If current number is prime
        if (p[i] != False) :
 
            # Store the prime
            prime.append(pow(i, 4))
 
# Function to count numbers in the
# range [L, R] having exactly 5 factors
def countNumbers(L, R) :
 
    # Stores the required count
    Count = 0
    for p in prime :
        if (p >= L and p <= R) :
            Count += 1
    print(Count)
 
# Driver Code
L = 16
R = 85000
Sieve()
countNumbers(L, R)
 
# This code is contributed by code_hunt.


C#
// C# Program to implement
// the above approach
using System;
class GFG
{
  static int N = 200000;
 
  // Stores all prime numbers
  // up to 2 * 10^5
  static int []prime = new int [20000];
  static int index = 0;
 
  // Function to generate all prime
  // numbers up to 2 * 10 ^ 5 using
  // Sieve of Eratosthenes
  static void Sieve()
  {
    index = 0;
    int []p = new int [N + 1];
    for(int i = 0; i <= N; i++)
    {
      p[i] = 1;
    }
 
    // Mark 0 and 1 as non-prime
    p[0] = p[1] = 0;
    for (int i = 2; i * i <= N; i++)
    {
 
      // If i is prime
      if (p[i] == 1)
      {
 
        // Mark all its factors as non-prime
        for (int j = i * i; j <= N; j += i)
        {
          p[j] = 0;
        }
      }
    }
    for (int i = 1; i < N; i++)
    {
 
      // If current number is prime
      if (p[i] == 1)
      {
 
        // Store the prime
        prime[index++] = (int)(Math.Pow(i, 4));
      }
    }
  }
 
  // Function to count numbers in the
  // range [L, R] having exactly 5 factors
  static void countNumbers(int L,int R)
  {
 
    // Stores the required count
    int Count = 0;
    for(int i = 0; i < index; i++)
    {
      int p = prime[i];
      if (p >= L && p <= R)
      {
        Count++;
      }
    }
    Console.WriteLine(Count);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int L = 16, R = 85000;
    Sieve();
    countNumbers(L, R);
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
7

时间复杂度: O(N * log(log(N)))
辅助空间: O(N)