给定两个整数N和K ,任务是为每组K个连续元素找到第一个元素,这些元素具有正好为K的素数并且小于N。
例子:
Input: N = 30, K = 2
Output: 14 20 21
Explanation:
Numbers which have prime factors equals to 2 less than 30 = { 14, 15, 18, 20, 21, 22, 24, 26, 28 }.
In the above examples, for K = 2 (14, 15) (20, 21) (21, 22) only forms such sets and therefore the series is 14, 20, 21.
Input: N = 1000, K = 3
Output: 644 740 804 986
天真的方法:天真的方法是从2迭代到N,并检查每K个连续数字形成一个集合,该集合中的每个元素都有K个素数。如果选择“是”,则打印该组的第一个元素并检查接下来的K个元素。
时间复杂度: O(N * K)
辅助空间: O(1)
高效方法:可以通过预先计算直到N的素数的数量并检查每K个连续元素计数中有K个素数的方法来优化上述方法。步骤如下:
- 创建一个最小素数数组spf [] ,使用Eratosthenes的Sieve,为每个数字存储最小素数,直到N。
- 使用上面的步骤,计算素数的数量,直到每个数N
- 将数字存储在素数为K的数组(例如result [] )中。
- 对于阵列结果的每个元素[]检查是否存在连续的K数,然后打印K个连续编号的第一个元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#define x 2000021
using namespace std;
// For storing smallest prime factor
long long int v[x];
// Function construct smallest
// prime factor array
void sieve()
{
v[1] = 1;
// Mark smallest prime factor for
// every number to be itself.
for (long long int i = 2; i < x; i++)
v[i] = i;
// separately mark spf for every even
// number as 2
for (long long int i = 4; i < x; i += 2)
v[i] = 2;
for (long long int i = 3; i * i < x; i++) {
// Check if i is prime
if (v[i] == i) {
// Mark SPF for all numbers
// divisible by i
for (long long int j = i * i;
j < x; j += i) {
// Mark spf[j] if it is
// not previously marked
if (v[j] == j) {
v[j] = i;
}
}
}
}
}
// Function for counts total number
// of prime factors
long long int prime_factors(long long n)
{
set s;
while (n != 1) {
s.insert(v[n]);
n = n / v[n];
}
return s.size();
}
// Function to print elements of sets
// of K consecutive elements having
// K prime factors
void distinctPrimes(long long int m,
long long int k)
{
// To store the result
vector result;
for (long long int i = 14;
i < m + k; i++) {
// Count number of prime
// factors of number
long long count
= prime_factors(i);
// If number has exactly K
// factors push in result[]
if (count == k) {
result.push_back(i);
}
}
long long int p = result.size();
for (long long int index = 0;
index < p - 1; index++) {
long long element = result[index];
long long count = 1, z = index;
// Iterate till we get K consecutive
// elements in result[]
while (z < p - 1 && count <= k
&& result[z] + 1
== result[z + 1]) {
// Count sequence until K
count++;
z++;
}
// Print the element if count >= K
if (count >= k)
cout << element << ' ';
}
}
// Driver Code
int main()
{
// To construct spf[]
sieve();
// Given N and K
long long int N = 1000, K = 3;
// Function Call
distinctPrimes(N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
static final int x = 2000021;
// For storing smallest prime factor
static int []v = new int[x];
// Function consmallest
// prime factor array
static void sieve()
{
v[1] = 1;
// Mark smallest prime factor for
// every number to be itself.
for(int i = 2; i < x; i++)
v[i] = i;
// Separately mark spf for every even
// number as 2
for(int i = 4; i < x; i += 2)
v[i] = 2;
for(int i = 3; i * i < x; i++)
{
// Check if i is prime
if (v[i] == i)
{
// Mark SPF for all numbers
// divisible by i
for(int j = i * i; j < x; j += i)
{
// Mark spf[j] if it is
// not previously marked
if (v[j] == j)
{
v[j] = i;
}
}
}
}
}
// Function for counts total number
// of prime factors
static int prime_factors(int n)
{
HashSet s = new HashSet();
while (n != 1)
{
s.add(v[n]);
n = n / v[n];
}
return s.size();
}
// Function to print elements of sets
// of K consecutive elements having
// K prime factors
static void distinctPrimes(int m, int k)
{
// To store the result
Vector result = new Vector();
for (int i = 14; i < m + k; i++)
{
// Count number of prime
// factors of number
long count = prime_factors(i);
// If number has exactly K
// factors push in result[]
if (count == k)
{
result.add(i);
}
}
int p = result.size();
for(int index = 0;
index < p - 1; index++)
{
long element = result.get(index);
int count = 1, z = index;
// Iterate till we get K consecutive
// elements in result[]
while (z < p - 1 && count <= k &&
result.get(z) + 1 ==
result.get(z + 1))
{
// Count sequence until K
count++;
z++;
}
// Print the element if count >= K
if (count >= k)
System.out.print(element + " ");
}
}
// Driver code
public static void main(String[] args)
{
// To construct spf[]
sieve();
// Given N and K
int N = 1000, K = 3;
// Function call
distinctPrimes(N, K);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program for the above approach
x = 2000021
# For storing smallest prime factor
v = [0] * x
# Function construct smallest
# prime factor array
def sieve():
v[1] = 1
# Mark smallest prime factor for
# every number to be itself
for i in range(2, x):
v[i] = i
# separately mark spf for every
# even number as 2
for i in range(4, x, 2):
v[i] = 2
i = 3
while (i * i < x):
# Check if i is prime
if (v[i] == i):
# Mark SPF for all numbers
# divisible by i
for j in range(i * i, x, i):
# Mark spf[i] if it is
# not previously marked
if (v[j] == j):
v[j] = i
i += 1
# Function for counts total number
# of prime factors
def prime_factors(n):
s = set()
while (n != 1):
s.add(v[n])
n = n // v[n]
return len(s)
# Function to print elements of sets
# of K consecutive elements having
# K prime factors
def distinctPrimes(m, k):
# To store the result
result = []
for i in range(14, m + k):
# Count number of prime
# factors of number
count = prime_factors(i)
# If number has exactly K
# factors puch in result[]
if (count == k):
result.append(i)
p = len(result)
for index in range(p - 1):
element = result[index]
count = 1
z = index
# Iterate till we get K consecutive
# elements in result[]
while (z < p - 1 and count <= k and
result[z] + 1 == result[z + 1]):
# Count sequence until K
count += 1
z += 1
# Print the element if count >= K
if (count >= k):
print(element, end = ' ')
# Driver Code
if __name__ == '__main__':
# To construct spf[]
sieve()
# Given N and K
N = 1000
K = 3
# Function call
distinctPrimes(N, K)
# This code is contributed by himanshu77
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
static readonly int x = 2000021;
// For storing smallest prime factor
static int []v = new int[x];
// Function consmallest
// prime factor array
static void sieve()
{
v[1] = 1;
// Mark smallest prime factor for
// every number to be itself.
for(int i = 2; i < x; i++)
v[i] = i;
// Separately mark spf for every even
// number as 2
for(int i = 4; i < x; i += 2)
v[i] = 2;
for(int i = 3; i * i < x; i++)
{
// Check if i is prime
if (v[i] == i)
{
// Mark SPF for all numbers
// divisible by i
for(int j = i * i; j < x; j += i)
{
// Mark spf[j] if it is
// not previously marked
if (v[j] == j)
{
v[j] = i;
}
}
}
}
}
// Function for counts total number
// of prime factors
static int prime_factors(int n)
{
HashSet s = new HashSet();
while (n != 1)
{
s.Add(v[n]);
n = n / v[n];
}
return s.Count;
}
// Function to print elements of sets
// of K consecutive elements having
// K prime factors
static void distinctPrimes(int m, int k)
{
// To store the result
List result = new List();
for (int i = 14; i < m + k; i++)
{
// Count number of prime
// factors of number
long count = prime_factors(i);
// If number has exactly K
// factors push in result[]
if (count == k)
{
result.Add(i);
}
}
int p = result.Count;
for(int index = 0;
index < p - 1; index++)
{
long element = result[index];
int count = 1, z = index;
// Iterate till we get K consecutive
// elements in result[]
while (z < p - 1 && count <= k &&
result[z] + 1 == result[z + 1])
{
// Count sequence until K
count++;
z++;
}
// Print the element if count >= K
if (count >= k)
Console.Write(element + " ");
}
}
// Driver code
public static void Main(String[] args)
{
// To construct spf[]
sieve();
// Given N and K
int N = 1000, K = 3;
// Function call
distinctPrimes(N, K);
}
}
// This code is contributed by Amit Katiyar
Javascript
输出:
644 740 804 986
时间复杂度: O(N * log(log N))
辅助空间: O(N)