给定两个整数L和R ,任务是计算范围[L, R]中恰好有5 个不同正因子的数字的计数。
例子:
Input: L = 1, R= 100
Output: 2
Explanation: The only two numbers in the range [1, 100] having exactly 5 prime factors are 16 and 81.
Factors of 16 are {1, 2, 4, 8, 16}.
Factors of 8 are {1, 3, 9, 27, 81}.
Input: L = 1, R= 100
Output: 2
朴素的方法:解决这个问题最简单的方法是遍历范围[L, R]并为每个数字计算其因子。如果因子数等于5 ,则将 count 增加1 。
时间复杂度: (R – L) × √N
辅助空间: O(1)
有效的方法:为了优化上述方法,需要对恰好具有 5 个因子的数字进行以下观察。
Let the prime factorization of a number be p1a1×p2a2 × … ×pnan.
Therefore, the count of factors of this number can be written as (a1 + 1)×(a2 + 1)× … ×(an + 1).
Since this product must be equal to 5 (which is a prime number), only one term greater than 1 must exist in the product. That term must be equal to 5.
Therefore, if ai + 1 = 5
=> ai = 4
请按照以下步骤解决问题:
- 所需的计数是包含 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)