给定两个整数K和N ,任务是从素数和为K的[2,N – 1]范围内找到整数的数目
例子:
Input: N = 20, K = 7
Output: 2
7 and 10 are the only valid numbers.
sumPFactors(7) = 7
sumPFactors(10) = 2 + 5 = 7
Input: N = 25, K = 5
Output: 5
的方法:创建一个数组sumPF []其中sumPF [I]存储我的素因数,可以使用本文中所使用的方法来容易地计算出的总和。现在,初始化一个变量count = 0并运行一个从2到N – 1的循环,如果sumPF [i] = K,则对每个元素i递增计数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define MAX 1000001
// Function to return the count of numbers
// below N whose sum of prime factors is K
int countNum(int N, int K)
{
// To store the sum of prime factors
// for all the numbers
int sumPF[MAX] = { 0 };
for (int i = 2; i < N; i++) {
// If i is prime
if (sumPF[i] == 0) {
// Add i to all the numbers
// which are divisible by i
for (int j = i; j < N; j += i) {
sumPF[j] += i;
}
}
}
// To store the count of required numbers
int count = 0;
for (int i = 2; i < N; i++) {
if (sumPF[i] == K)
count++;
}
// Return the required count
return count;
}
// Driver code
int main()
{
int N = 20, K = 7;
cout << countNum(N, K);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static int MAX = 1000001;
// Function to return the count of numbers
// below N whose sum of prime factors is K
static int countNum(int N, int K)
{
// To store the sum of prime factors
// for all the numbers
int []sumPF = new int[MAX];
for (int i = 2; i < N; i++)
{
// If i is prime
if (sumPF[i] == 0)
{
// Add i to all the numbers
// which are divisible by i
for (int j = i; j < N; j += i)
{
sumPF[j] += i;
}
}
}
// To store the count of required numbers
int count = 0;
for (int i = 2; i < N; i++)
{
if (sumPF[i] == K)
count++;
}
// Return the required count
return count;
}
// Driver code
public static void main(String[] args)
{
int N = 20, K = 7;
System.out.println(countNum(N, K));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
MAX = 1000001
# Function to return the count of numbers
# below N whose sum of prime factors is K
def countNum(N, K) :
# To store the sum of prime factors
# for all the numbers
sumPF = [0] * MAX;
for i in range(2, N) :
# If i is prime
if (sumPF[i] == 0) :
# Add i to all the numbers
# which are divisible by i
for j in range(i, N, i) :
sumPF[j] += i;
# To store the count of required numbers
count = 0;
for i in range(2, N) :
if (sumPF[i] == K) :
count += 1;
# Return the required count
return count;
# Driver code
if __name__ == "__main__" :
N = 20; K = 7;
print(countNum(N, K));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
static int MAX = 1000001;
// Function to return the count of numbers
// below N whose sum of prime factors is K
static int countNum(int N, int K)
{
// To store the sum of prime factors
// for all the numbers
int []sumPF = new int[MAX];
for (int i = 2; i < N; i++)
{
// If i is prime
if (sumPF[i] == 0)
{
// Add i to all the numbers
// which are divisible by i
for (int j = i; j < N; j += i)
{
sumPF[j] += i;
}
}
}
// To store the count of required numbers
int count = 0;
for (int i = 2; i < N; i++)
{
if (sumPF[i] == K)
count++;
}
// Return the required count
return count;
}
// Driver code
public static void Main(String[] args)
{
int N = 20, K = 7;
Console.WriteLine(countNum(N, K));
}
}
// This code is contributed by 29AjayKumar
输出:
2