Computer >> Computer tutorials >  >> Programming >> C++

Count numbers < = N whose difference with the count of primes upto them is > = K in C++


Given two integers N and K, the goal is to find the count of numbers such that they follow below conditions −

  • Number<=N

  • | Number−count | >=K Where count is the number of prime numbers less than or equal to Number.

For Example

Input

N = 5, K = 2

Output

Count of numbers < = N whose difference with the count of primes upto them is > = K are: 2

Explanation

The numbers that follow the conditions are:
5 ( 5−2>=2 ) and 4 ( 4−2>=2 )

Input

N = 10, K = 6

Output

Count of numbers < = N whose difference with the count of primes upto them
is > = K are: 1

Explanation

The numbers that follow the conditions are:
10 ( 10−4>=6 )

Approach used in the below program is as follows −

In this approach we will use binary search to reduce our calculations. If the count of prime numbers upto num is count1 and for number num+1 this count is count2. Then the difference num+1−count2 >= num−count1. So if num is valid, num+1 will also be valid.For the first number found, say ‘num’ using binary search that follows the condition, then ‘num’+1 would also follow the same condition. In this way all the numbers between num to N will be counted.

  • Take variables N and K as input.

  • Array arr[] is used to store the count of prime numbers upto i will be stored at index i.

  • Function set_prime() updates array arr[] for storing the counts of primes.

  • Array check[i] stores true if i is prime else stores false.

  • Set check[0]=check[1] = false as they are non primes.

  • Traverse check from index i=2 to i*i<size(1000001). And if any check[i] is 1, number is prime then set all check[j] with 0 from j=i*2 to j<size.

  • Now traverse arr[] using for loop and update it. All counts upto arr[i]=arr[i−1]. If arr[i] itself is prime then that count will increase by 1. Set arr[i]++.

  • Function total(int N, int K) takes N and K and returns Count of numbers < = N whose difference with the count of primes upto them is > = K.

  • Call set_prime().

  • Take temp_1=1 and temp_2=N. Take the initial count as 0.

  • Now using binary search, in while loop take set = (temp_1 + temp_2) >> 1 ((first+last) /2 ).

  • If set−arr[set] is >=K then condition is met, update count with set and temp_2=set−1.

  • Otherwise set temp_1=temp_1+1.

  • At the end set count as minimum valid number N−count+1 or 0.

  • At the end of all loops return count as result.

Example

#include <bits/stdc++.h>
using namespace std;
#define size 1000001
int arr[size];
void set_prime(){
   bool check[size];
   memset(check, 1, sizeof(check));
   check[0] = 0;
   check[1] = 0;
   for (int i = 2; i * i < size; i++){
      if(check[i] == 1){
         for (int j = i * 2; j < size; j += i){
            check[j] = 0;
         }
      }
   }
   for (int i = 1; i < size; i++){
      arr[i] = arr[i − 1];
      if(check[i] == 1){
         arr[i]++;
      }
   }
}
int total(int N, int K){
   set_prime();
   int temp_1 = 1;
   int temp_2 = N;
   int count = 0;
   while (temp_1 <= temp_2){
      int set = (temp_1 + temp_2) >> 1;
      if (set − arr[set] >= K){
         count = set;
         temp_2 = set − 1;
      } else {
         temp_1 = set + 1;
      }
   }
   count = (count ? N − count + 1 : 0);
   return count;
}
int main(){
   int N = 12, K = 5;
   cout<<"Count of numbers < = N whose difference with the count of primes upto them is > = K are: "<<total(N, K);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of numbers < = N whose difference with the count of primes upto them is > = K are: 4