给定两个整数N和K ,任务是检查N是否可以表示为K个正整数的总和,其中至少K – 1个它们几乎是质数。
Nearly Primes: Refers to those numbers which can be represented as a product of any pair of prime numbers.
例子:
Input: N = 100, K = 6
Output: Yes
Explanation: 100 can be represented as 4 + 6 + 9 + 10 + 14 + 57, where 4 (= 2 * 2), 6 ( = 3 * 2), 9 ( = 3 * 3), 10 ( = 5 * 2) and 14 ( = 7 * 2) are nearly primes.
Input: N=19, K = 4
Output: No
方法:想法是找到第一个K – 1个近似质数的总和,并检查其值是否小于或等于N。如果发现是真的,则打印“是” 。否则,打印No。
请按照以下步骤解决问题:
- 将前K – 1个近质数的总和存储在变量中,例如S。
- 从2进行迭代,直到获得S并执行以下步骤:
- 检查当前数素数的计数是否等于2 。
- 如果发现为真,则将当前数字的值添加到S。
- 如果这些数字的计数等于K – 1 ,请跳出循环。
- 检查S> = N的值。如果发现是真的,请打印“是” 。
- 否则,打印编号。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count all prime
// factors of a given number
int countPrimeFactors(int n)
{
int count = 0;
// Count the number of 2s
// that divides n
while (n % 2 == 0) {
n = n / 2;
count++;
}
// Since n is odd at this point,
// skip one element
for (int i = 3; i <= sqrt(n); i = i + 2) {
// While i divides n, count
// i and divide n
while (n % i == 0) {
n = n / i;
count++;
}
}
// If n is a prime number
// greater than 2
if (n > 2)
count++;
return (count);
}
// Function to find the sum of
// first n nearly prime numbers
int findSum(int n)
{
// Store the required sum
int sum = 0;
for (int i = 1, num = 2; i <= n; num++) {
// Add this number if it is
// satisfies the condition
if (countPrimeFactors(num) == 2) {
sum += num;
// Increment count of
// nearly prime numbers
i++;
}
}
return sum;
}
// Function to check if N can be
// represented as sum of K different
// positive integers out of which at
// least K - 1 of them are nearly prime
void check(int n, int k)
{
// Store the sum of first
// K - 1 nearly prime numbers
int s = findSum(k - 1);
// If sum is greater
// than or equal to n
if (s >= n)
cout << "No";
// Otherwise, print Yes
else
cout << "Yes";
}
// Driver Code
int main()
{
int n = 100, k = 6;
check(n, k);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count all prime
// factors of a given number
static int countPrimeFactors(int n)
{
int count = 0;
// Count the number of 2s
// that divides n
while (n % 2 == 0)
{
n = n / 2;
count++;
}
// Since n is odd at this point,
// skip one element
for(int i = 3;
i <= (int)Math.sqrt(n);
i = i + 2)
{
// While i divides n, count
// i and divide n
while (n % i == 0)
{
n = n / i;
count++;
}
}
// If n is a prime number
// greater than 2
if (n > 2)
count++;
return (count);
}
// Function to find the sum of
// first n nearly prime numbers
static int findSum(int n)
{
// Store the required sum
int sum = 0;
for(int i = 1, num = 2; i <= n; num++)
{
// Add this number if it is
// satisfies the condition
if (countPrimeFactors(num) == 2)
{
sum += num;
// Increment count of
// nearly prime numbers
i++;
}
}
return sum;
}
// Function to check if N can be
// represented as sum of K different
// positive integers out of which at
// least K - 1 of them are nearly prime
static void check(int n, int k)
{
// Store the sum of first
// K - 1 nearly prime numbers
int s = findSum(k - 1);
// If sum is greater
// than or equal to n
if (s >= n)
System.out.print("No");
// Otherwise, print Yes
else
System.out.print("Yes");
}
// Driver Code
public static void main(String[] args)
{
int n = 100, k = 6;
check(n, k);
}
}
// This code is contributed by splevel62
Python3
# Python3 program for the above approach
import math
# Function to count all prime
# factors of a given number
def countPrimeFactors(n) :
count = 0
# Count the number of 2s
# that divides n
while (n % 2 == 0) :
n = n // 2
count += 1
# Since n is odd at this point,
# skip one element
for i in range(3, int(math.sqrt(n) + 1), 2) :
# While i divides n, count
# i and divide n
while (n % i == 0) :
n = n // i
count += 1
# If n is a prime number
# greater than 2
if (n > 2) :
count += 1
return (count)
# Function to find the sum of
# first n nearly prime numbers
def findSum(n) :
# Store the required sum
sum = 0
i = 1
num = 2
while(i <= n) :
# Add this number if it is
# satisfies the condition
if (countPrimeFactors(num) == 2) :
sum += num
# Increment count of
# nearly prime numbers
i += 1
num += 1
return sum
# Function to check if N can be
# represented as sum of K different
# positive integers out of which at
# least K - 1 of them are nearly prime
def check(n, k) :
# Store the sum of first
# K - 1 nearly prime numbers
s = findSum(k - 1)
# If sum is great
# than or equal to n
if (s >= n) :
print("No")
# Otherwise, prYes
else :
print("Yes")
# Driver Code
n = 100
k = 6
check(n, k)
# This code is contributed by susmitakundugoaldanga.
C#
// C# program for above approach
using System;
public class GFG
{
// Function to count all prime
// factors of a given number
static int countPrimeFactors(int n)
{
int count = 0;
// Count the number of 2s
// that divides n
while (n % 2 == 0)
{
n = n / 2;
count++;
}
// Since n is odd at this point,
// skip one element
for(int i = 3;
i <= (int)Math.Sqrt(n);
i = i + 2)
{
// While i divides n, count
// i and divide n
while (n % i == 0)
{
n = n / i;
count++;
}
}
// If n is a prime number
// greater than 2
if (n > 2)
count++;
return (count);
}
// Function to find the sum of
// first n nearly prime numbers
static int findSum(int n)
{
// Store the required sum
int sum = 0;
for(int i = 1, num = 2; i <= n; num++)
{
// Add this number if it is
// satisfies the condition
if (countPrimeFactors(num) == 2)
{
sum += num;
// Increment count of
// nearly prime numbers
i++;
}
}
return sum;
}
// Function to check if N can be
// represented as sum of K different
// positive integers out of which at
// least K - 1 of them are nearly prime
static void check(int n, int k)
{
// Store the sum of first
// K - 1 nearly prime numbers
int s = findSum(k - 1);
// If sum is greater
// than or equal to n
if (s >= n)
Console.WriteLine("No");
// Otherwise, print Yes
else
Console.WriteLine("Yes");
}
// Driver code
public static void Main(String[] args)
{
int n = 100, k = 6;
check(n, k);
}
}
// This code is contributed by splevel62.
Javascript
输出:
Yes
时间复杂度: O(K *√X),其中X是第(K – 1)个近质数。
辅助空间: O(1)