Check if a number can be represented as sum of K positive integers out of which at least K – 1 are nearly prime
Given two integers N and K, the task is to check if N can be represented as a sum of K positive integers, where at least K – 1 of them are nearly prime.
Nearly Primes: Refers to those numbers which can be represented as a product of any pair of prime numbers.
Examples:
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
Approach: The idea is to find the sum of the first K – 1 nearly prime numbers and check if its value is less than or equal to N or not. If found to be true, then print Yes. Otherwise, print No.
Follow the steps below to solve the problem:
- Store the sum of the first K – 1 nearly prime numbers in a variable, say S.
- Iterate from 2, until S is obtained and perform the following steps:
- Check if the value of S>=N. If found to be true, print Yes.
- Otherwise, print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPrimeFactors( int n)
{
int count = 0;
while (n % 2 == 0) {
n = n / 2;
count++;
}
for ( int i = 3; i <= sqrt (n); i = i + 2) {
while (n % i == 0) {
n = n / i;
count++;
}
}
if (n > 2)
count++;
return (count);
}
int findSum( int n)
{
int sum = 0;
for ( int i = 1, num = 2; i <= n; num++) {
if (countPrimeFactors(num) == 2) {
sum += num;
i++;
}
}
return sum;
}
void check( int n, int k)
{
int s = findSum(k - 1);
if (s >= n)
cout << "No" ;
else
cout << "Yes" ;
}
int main()
{
int n = 100, k = 6;
check(n, k);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countPrimeFactors( int n)
{
int count = 0 ;
while (n % 2 == 0 )
{
n = n / 2 ;
count++;
}
for ( int i = 3 ;
i <= ( int )Math.sqrt(n);
i = i + 2 )
{
while (n % i == 0 )
{
n = n / i;
count++;
}
}
if (n > 2 )
count++;
return (count);
}
static int findSum( int n)
{
int sum = 0 ;
for ( int i = 1 , num = 2 ; i <= n; num++)
{
if (countPrimeFactors(num) == 2 )
{
sum += num;
i++;
}
}
return sum;
}
static void check( int n, int k)
{
int s = findSum(k - 1 );
if (s >= n)
System.out.print( "No" );
else
System.out.print( "Yes" );
}
public static void main(String[] args)
{
int n = 100 , k = 6 ;
check(n, k);
}
}
|
Python3
import math
def countPrimeFactors(n) :
count = 0
while (n % 2 = = 0 ) :
n = n / / 2
count + = 1
for i in range ( 3 , int (math.sqrt(n) + 1 ), 2 ) :
while (n % i = = 0 ) :
n = n / / i
count + = 1
if (n > 2 ) :
count + = 1
return (count)
def findSum(n) :
sum = 0
i = 1
num = 2
while (i < = n) :
if (countPrimeFactors(num) = = 2 ) :
sum + = num
i + = 1
num + = 1
return sum
def check(n, k) :
s = findSum(k - 1 )
if (s > = n) :
print ( "No" )
else :
print ( "Yes" )
n = 100
k = 6
check(n, k)
|
C#
using System;
public class GFG
{
static int countPrimeFactors( int n)
{
int count = 0;
while (n % 2 == 0)
{
n = n / 2;
count++;
}
for ( int i = 3;
i <= ( int )Math.Sqrt(n);
i = i + 2)
{
while (n % i == 0)
{
n = n / i;
count++;
}
}
if (n > 2)
count++;
return (count);
}
static int findSum( int n)
{
int sum = 0;
for ( int i = 1, num = 2; i <= n; num++)
{
if (countPrimeFactors(num) == 2)
{
sum += num;
i++;
}
}
return sum;
}
static void check( int n, int k)
{
int s = findSum(k - 1);
if (s >= n)
Console.WriteLine( "No" );
else
Console.WriteLine( "Yes" );
}
public static void Main(String[] args)
{
int n = 100, k = 6;
check(n, k);
}
}
|
Javascript
<script>
function countPrimeFactors(n)
{
var count = 0;
while (n % 2 == 0)
{
n = parseInt(n / 2);
count++;
}
for (i = 3;
i <= parseInt(Math.sqrt(n));
i = i + 2)
{
while (n % i == 0)
{
n = parseInt(n / i);
count++;
}
}
if (n > 2)
count++;
return (count);
}
function findSum(n)
{
var sum = 0;
for (i = 1, num = 2; i <= n; num++)
{
if (countPrimeFactors(num) == 2)
{
sum += num;
i++;
}
}
return sum;
}
function check(n, k)
{
var s = findSum(k - 1);
if (s >= n)
document.write( "No" );
else
document.write( "Yes" );
}
var n = 100, k = 6;
check(n, k);
</script>
|
Time Complexity: O(K * √X), where X is the (K – 1)th nearly prime number.
Auxiliary Space: O(1)
Approach 2: Dynamic Programming:
Dynamic programming (DP) is used in the given code to efficiently calculate the sum of the first N nearly prime numbers.
- The idea behind DP is to store the solutions to subproblems and reuse them as needed to solve larger problems. In this case, the subproblem is finding the sum of the first k nearly prime numbers, where k is less than N. Once we have solved this subproblem, we can use the solution to compute the sum of the first k+1 nearly prime numbers, and so on until we have computed the sum of the first N nearly prime numbers.
- The DP array is represented by the vector dp, which has size N+1 and is initialized with -1 values. The value of dp[N] stores the sum of the first N nearly prime numbers. If dp[N] is already calculated, we can return it directly without recomputing it. Otherwise, we compute dp[N] recursively by first calculating dp[N-1], then checking if N*2-1 is nearly prime, and adding it to dp[N-1] if it is. Finally, we update dp[N] with the computed value and return it.
- Using DP in this way avoids recomputing the sum of the first k nearly prime numbers multiple times for different values of k, which can be computationally expensive. Instead, we only need to compute each value once and store it for later use. This significantly improves the efficiency of the algorithm.
Here is the Code of Above Approach:
C++
#include<bits/stdc++.h>
using namespace std;
int countPrimeFactors( int n)
{
int count = 0;
while (n % 2 == 0) {
n = n / 2;
count++;
}
for ( int i = 3; i <= sqrt (n); i = i + 2) {
while (n % i == 0) {
n = n / i;
count++;
}
}
if (n > 2)
count++;
return (count);
}
int findSum( int n, vector< int >& dp)
{
if (dp[n] != -1)
return dp[n];
if (n == 0)
return 0;
if (n == 1)
return 2;
int sum = findSum(n-1, dp);
if (countPrimeFactors(n*2-1) == 2)
sum += n*2-1;
dp[n] = sum;
return sum;
}
void check( int n, int k)
{
vector< int > dp(n+1, -1);
int s = findSum(k - 1, dp);
if (s >= n)
cout << "No" ;
else
cout << "Yes" ;
}
int main()
{
int n = 100, k = 6;
check(n, k);
return 0;
}
|
Java
import java.util.*;
public class NearlyPrimeSum {
static int countPrimeFactors( int n) {
int count = 0 ;
while (n % 2 == 0 ) {
n = n / 2 ;
count++;
}
for ( int i = 3 ; i <= Math.sqrt(n); i = i + 2 ) {
while (n % i == 0 ) {
n = n / i;
count++;
}
}
if (n > 2 )
count++;
return count;
}
static int findSum( int n, int [] dp) {
if (dp[n] != - 1 )
return dp[n];
if (n == 0 )
return 0 ;
if (n == 1 )
return 2 ;
int sum = findSum(n - 1 , dp);
if (countPrimeFactors(n * 2 - 1 ) == 2 )
sum += n * 2 - 1 ;
dp[n] = sum;
return sum;
}
static void check( int n, int k) {
int [] dp = new int [n + 1 ];
Arrays.fill(dp, - 1 );
int s = findSum(k - 1 , dp);
if (s >= n)
System.out.println( "No" );
else
System.out.println( "Yes" );
}
public static void main(String[] args) {
int n = 100 , k = 6 ;
check(n, k);
}
}
|
Python3
import math
def count_prime_factors(n):
count = 0
while n % 2 = = 0 :
n / / = 2
count + = 1
for i in range ( 3 , int (math.sqrt(n)) + 1 , 2 ):
while n % i = = 0 :
n / / = i
count + = 1
if n > 2 :
count + = 1
return count
def find_sum(n, dp):
if dp[n] ! = - 1 :
return dp[n]
if n = = 0 :
return 0
if n = = 1 :
return 2
sum = find_sum(n - 1 , dp)
if count_prime_factors(n * 2 - 1 ) = = 2 :
sum + = n * 2 - 1
dp[n] = sum
return sum
def check(n, k):
dp = [ - 1 ] * (n + 1 )
s = find_sum(k - 1 , dp)
if s > = n:
print ( "No" )
else :
print ( "Yes" )
if __name__ = = "__main__" :
n = 100
k = 6
check(n, k)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int CountPrimeFactors( int n)
{
int count = 0;
while (n % 2 == 0)
{
n = n / 2;
count++;
}
for ( int i = 3; i <= Math.Sqrt(n); i = i + 2)
{
while (n % i == 0)
{
n = n / i;
count++;
}
}
if (n > 2)
count++;
return count;
}
static int FindSum( int n, List< int > dp)
{
if (dp[n] != -1)
return dp[n];
if (n == 0)
return 0;
if (n == 1)
return 2;
int sum = FindSum(n - 1, dp);
if (CountPrimeFactors(n * 2 - 1) == 2)
sum += n * 2 - 1;
dp[n] = sum;
return sum;
}
static void Check( int n, int k)
{
List< int > dp = new List< int >( new int [n + 1]);
for ( int i = 0; i <= n; i++)
{
dp[i] = -1;
}
int s = FindSum(k - 1, dp);
if (s >= n)
Console.WriteLine( "No" );
else
Console.WriteLine( "Yes" );
}
static void Main()
{
int n = 100, k = 6;
Check(n, k);
}
}
|
Javascript
function countPrimeFactors(n) {
let count = 0;
while (n % 2 === 0) {
n = Math.floor(n / 2);
count++;
}
for (let i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i === 0) {
n = Math.floor(n / i);
count++;
}
}
if (n > 2) {
count++;
}
return count;
}
function findSum(n, dp) {
if (dp[n] !== -1) {
return dp[n];
}
if (n === 0) {
return 0;
}
if (n === 1) {
return 2;
}
let sum = findSum(n - 1, dp);
if (countPrimeFactors(n * 2 - 1) === 2) {
sum += n * 2 - 1;
}
dp[n] = sum;
return sum;
}
function check(n, k) {
let dp = new Array(n + 1).fill(-1);
let s = findSum(k - 1, dp);
if (s >= n) {
console.log( "No" );
} else {
console.log( "Yes" );
}
}
const n = 100;
const k = 6;
check(n, k);
|
Time Complexity: O(n^2 * sqrt(n)), where n is the nearly prime number.
Auxiliary Space: O(n), where n is the size of the DP array.
Approach:
- This approach checks if a number is prime by iterating up to the square root of the number and checks for divisibility.
- The countPrimeFactors function counts the number of prime factors of a given number.
- The findSum function generates the first n nearly prime numbers and calculates their sum.
- Finally, the check function compares the sum with the given number to determine if it can be represented as a sum of K different positive integers, with at least K – 1 of them being nearly prime.
Here is the code of above approach:
C++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
bool isPrime( int num) {
if (num < 2)
return false ;
for ( int i = 2; i <= sqrt (num); i++) {
if (num % i == 0)
return false ;
}
return true ;
}
int countPrimeFactors( int n) {
int count = 0;
for ( int i = 2; i <= n; i++) {
if (isPrime(i) && n % i == 0)
count++;
}
return count;
}
int findSum( int n) {
vector< int > nearlyPrimes;
int num = 2;
while (nearlyPrimes.size() < n) {
if (countPrimeFactors(num) == 2)
nearlyPrimes.push_back(num);
num++;
}
int sum = 0;
for ( int i = 0; i < n; i++)
sum += nearlyPrimes[i];
return sum;
}
void check( int n, int k) {
int s = findSum(k - 1);
if (s >= n)
cout << "No" ;
else
cout << "Yes" ;
}
int main() {
int n = 100, k = 6;
check(n, k);
return 0;
}
|
Java
import java.util.ArrayList;
public class NearlyPrime {
static boolean isPrime( int num) {
if (num < 2 )
return false ;
for ( int i = 2 ; i <= Math.sqrt(num); i++) {
if (num % i == 0 )
return false ;
}
return true ;
}
static int countPrimeFactors( int n) {
int count = 0 ;
for ( int i = 2 ; i <= n; i++) {
if (isPrime(i) && n % i == 0 )
count++;
}
return count;
}
static int findSum( int n) {
ArrayList<Integer> nearlyPrimes = new ArrayList<>();
int num = 2 ;
while (nearlyPrimes.size() < n) {
if (countPrimeFactors(num) == 2 )
nearlyPrimes.add(num);
num++;
}
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += nearlyPrimes.get(i);
return sum;
}
static void check( int n, int k) {
int s = findSum(k - 1 );
if (s >= n)
System.out.println( "No" );
else
System.out.println( "Yes" );
}
public static void main(String[] args) {
int n = 100 , k = 6 ;
check(n, k);
}
}
|
Python
import math
def is_prime(num):
if num < 2 :
return False
for i in range ( 2 , int (math.sqrt(num)) + 1 ):
if num % i = = 0 :
return False
return True
def count_prime_factors(n):
count = 0
for i in range ( 2 , n + 1 ):
if is_prime(i) and n % i = = 0 :
count + = 1
return count
def find_sum(n):
nearly_primes = []
num = 2
while len (nearly_primes) < n:
if count_prime_factors(num) = = 2 :
nearly_primes.append(num)
num + = 1
sum_ = sum (nearly_primes[:n])
return sum_
def check(n, k):
s = find_sum(k - 1 )
if s > = n:
print ( "No" )
else :
print ( "Yes" )
if __name__ = = "__main__" :
n = 100
k = 6
check(n, k)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static bool IsPrime( int num)
{
if (num < 2)
return false ;
for ( int i = 2; i <= Math.Sqrt(num); i++)
{
if (num % i == 0)
return false ;
}
return true ;
}
static int CountPrimeFactors( int n)
{
int count = 0;
for ( int i = 2; i <= n; i++)
{
if (IsPrime(i) && n % i == 0)
count++;
}
return count;
}
static int FindSum( int n)
{
List< int > nearlyPrimes = new List< int >();
int num = 2;
while (nearlyPrimes.Count < n)
{
if (CountPrimeFactors(num) == 2)
nearlyPrimes.Add(num);
num++;
}
int sum = 0;
for ( int i = 0; i < n; i++)
sum += nearlyPrimes[i];
return sum;
}
static void Check( int n, int k)
{
int s = FindSum(k - 1);
if (s >= n)
Console.WriteLine( "No" );
else
Console.WriteLine( "Yes" );
}
static void Main()
{
int n = 100, k = 6;
Check(n, k);
}
}
|
Javascript
function isPrime(num) {
if (num < 2)
return false ;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0)
return false ;
}
return true ;
}
function countPrimeFactors(n) {
let count = 0;
for (let i = 2; i <= n; i++) {
if (isPrime(i) && n % i === 0)
count++;
}
return count;
}
function findSum(n) {
const nearlyPrimes = [];
let num = 2;
while (nearlyPrimes.length < n) {
if (countPrimeFactors(num) === 2)
nearlyPrimes.push(num);
num++;
}
const sum = nearlyPrimes.reduce((acc, current) => acc + current, 0);
return sum;
}
function check(n, k) {
const s = findSum(k - 1);
console.log(s >= n ? "No" : "Yes" );
}
const n = 100, k = 6;
check(n, k);
|
Output:
Yes
Time Complexity: O(K * √X), where X is the (K – 1)th nearly prime number.
Auxiliary Space: O(1)
Last Updated :
04 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...