Count numbers < = N whose difference with the count of primes upto them is > = K
Last Updated :
24 Jun, 2022
Given two positive integers N and K, the task is to count all the numbers that satisfy the following conditions:
If the number is num,
- num ? N.
- abs(num - count) ? K where count is the count of primes upto num.
Examples:
Input: N = 10, K = 3
Output: 5
6, 7, 8, 9 and 10 are the valid numbers. For 6, the difference between 6 and prime numbers upto 6 (2, 3, 5) is 3 i.e. 6 - 3 = 3. For 7, 8, 9 and 10 the differences are 3, 4, 5 and 6 respectively which are ? K.
Input: N = 30, K = 13
Output: 10
Prerequisite: Binary Search
Approach: Observe that the function which is the difference of the number and count of prime numbers upto that number is a monotonically increasing function for a particular K. Also, if a number X is a valid number then X + 1 will also be a valid number.
Proof :
Let the function Ci denotes the count of prime numbers upto number i. Now,
for the number X + 1 the difference is X + 1 - CX + 1 which is greater than
or equal to the difference X - CX for the number X, i.e. (X + 1 - CX + 1) ? (X - CX).
Thus, if (X - CX) ? S, then (X + 1 - CX + 1) ? S.
Hence, we can use binary search to find the minimum valid number X and all the numbers from X to N will be valid numbers. So, the answer would be N - X + 1.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000001;
// primeUpto[i] denotes count of prime
// numbers upto i
int primeUpto[MAX];
// Function to compute all prime numbers
// and update primeUpto array
void SieveOfEratosthenes()
{
bool isPrime[MAX];
memset(isPrime, 1, sizeof(isPrime));
// 0 and 1 are not primes
isPrime[0] = isPrime[1] = 0;
for (int i = 2; i * i < MAX; i++) {
// If i is prime
if (isPrime[i]) {
// Set all multiples of i as non-prime
for (int j = i * 2; j < MAX; j += i)
isPrime[j] = 0;
}
}
// Compute primeUpto array
for (int i = 1; i < MAX; i++) {
primeUpto[i] = primeUpto[i - 1];
if (isPrime[i])
primeUpto[i]++;
}
}
// Function to return the count
// of valid numbers
int countOfNumbers(int N, int K)
{
// Compute primeUpto array
SieveOfEratosthenes();
int low = 1, high = N, ans = 0;
while (low <= high) {
int mid = (low + high) >> 1;
// Check if the number is
// valid, try to reduce it
if (mid - primeUpto[mid] >= K) {
ans = mid;
high = mid - 1;
}
else
low = mid + 1;
}
// ans is the minimum valid number
return (ans ? N - ans + 1 : 0);
}
// Driver Code
int main()
{
int N = 10, K = 3;
cout << countOfNumbers(N, K);
}
Java
// Java implementation of the above approach
public class GFG{
static final int MAX = 1000001;
// primeUpto[i] denotes count of prime
// numbers upto i
static int primeUpto[] = new int [MAX];
// Function to compute all prime numbers
// and update primeUpto array
static void SieveOfEratosthenes()
{
int isPrime[] = new int[MAX];
for (int i=0; i < MAX ; i++ )
isPrime[i] = 1;
// 0 and 1 are not primes
isPrime[0] = isPrime[1] = 0;
for (int i = 2; i * i < MAX; i++) {
// If i is prime
if (isPrime[i] == 1) {
// Set all multiples of i as non-prime
for (int j = i * 2; j < MAX; j += i)
isPrime[j] = 0;
}
}
// Compute primeUpto array
for (int i = 1; i < MAX; i++) {
primeUpto[i] = primeUpto[i - 1];
if (isPrime[i] == 1)
primeUpto[i]++;
}
}
// Function to return the count
// of valid numbers
static int countOfNumbers(int N, int K)
{
// Compute primeUpto array
SieveOfEratosthenes();
int low = 1, high = N, ans = 0;
while (low <= high) {
int mid = (low + high) >> 1;
// Check if the number is
// valid, try to reduce it
if (mid - primeUpto[mid] >= K) {
ans = mid;
high = mid - 1;
}
else
low = mid + 1;
}
ans = ans != 0 ? N - ans + 1 : 0 ;
// ans is the minimum valid number
return ans ;
}
// Driver Code
public static void main(String []args)
{
int N = 10, K = 3;
System.out.println(countOfNumbers(N, K)) ;
}
// This code is contributed by Ryuga
}
Python3
# Python3 implementation of the above approach
MAX = 1000001
MAX_sqrt = MAX ** (0.5)
# primeUpto[i] denotes count of prime
# numbers upto i
primeUpto = [0] * (MAX)
# Function to compute all prime numbers
# and update primeUpto array
def SieveOfEratosthenes():
isPrime = [1] * (MAX)
# 0 and 1 are not primes
isPrime[0], isPrime[1] = 0, 0
for i in range(2, int(MAX_sqrt)):
# If i is prime
if isPrime[i] == 1:
# Set all multiples of i as non-prime
for j in range(i * 2, MAX, i):
isPrime[j] = 0
# Compute primeUpto array
for i in range(1, MAX):
primeUpto[i] = primeUpto[i - 1]
if isPrime[i] == 1:
primeUpto[i] += 1
# Function to return the count
# of valid numbers
def countOfNumbers(N, K):
# Compute primeUpto array
SieveOfEratosthenes()
low, high, ans = 1, N, 0
while low <= high:
mid = (low + high) >> 1
# Check if the number is
# valid, try to reduce it
if mid - primeUpto[mid] >= K:
ans = mid
high = mid - 1
else:
low = mid + 1
# ans is the minimum valid number
return (N - ans + 1) if ans else 0
# Driver Code
if __name__ == "__main__":
N, K = 10, 3
print(countOfNumbers(N, K))
# This code is contributed by Rituraj Jain
C#
// C# implementation of the above approach
using System;
public class GFG{
static int MAX = 1000001;
// primeUpto[i] denotes count of prime
// numbers upto i
static int []primeUpto = new int [MAX];
// Function to compute all prime numbers
// and update primeUpto array
static void SieveOfEratosthenes()
{
int []isPrime = new int[MAX];
for (int i=0; i < MAX ; i++ )
isPrime[i] = 1;
// 0 and 1 are not primes
isPrime[0] = isPrime[1] = 0;
for (int i = 2; i * i < MAX; i++) {
// If i is prime
if (isPrime[i] == 1) {
// Set all multiples of i as non-prime
for (int j = i * 2; j < MAX; j += i)
isPrime[j] = 0;
}
}
// Compute primeUpto array
for (int i = 1; i < MAX; i++) {
primeUpto[i] = primeUpto[i - 1];
if (isPrime[i] == 1)
primeUpto[i]++;
}
}
// Function to return the count
// of valid numbers
static int countOfNumbers(int N, int K)
{
// Compute primeUpto array
SieveOfEratosthenes();
int low = 1, high = N, ans = 0;
while (low <= high) {
int mid = (low + high) >> 1;
// Check if the number is
// valid, try to reduce it
if (mid - primeUpto[mid] >= K) {
ans = mid;
high = mid - 1;
}
else
low = mid + 1;
}
ans = ans != 0 ? N - ans + 1 : 0 ;
// ans is the minimum valid number
return ans ;
}
// Driver Code
public static void Main()
{
int N = 10, K = 3;
Console.WriteLine(countOfNumbers(N, K)) ;
}
// This code is contributed by anuj_67..
}
PHP
<?php
// PHP implementation of the above approach
$MAX = 100001;
// primeUpto[i] denotes count of
// prime numbers upto i
$primeUpto = array_fill(0, $MAX, 0);
// Function to compute all prime numbers
// and update primeUpto array
function SieveOfEratosthenes()
{
global $MAX,$primeUpto;
$isPrime = array_fill(0, $MAX, true);
// 0 and 1 are not primes
$isPrime[0] = $isPrime[1] = false;
for ($i = 2; $i * $i < $MAX; $i++)
{
// If i is prime
if ($isPrime[$i])
{
// Set all multiples of i as non-prime
for ($j = $i * 2; $j < $MAX; $j += $i)
$isPrime[$j] = false;
}
}
// Compute primeUpto array
for ($i = 1; $i < $MAX; $i++)
{
$primeUpto[$i] = $primeUpto[$i - 1];
if ($isPrime[$i])
$primeUpto[$i]++;
}
}
// Function to return the count
// of valid numbers
function countOfNumbers($N, $K)
{
// Compute primeUpto array
SieveOfEratosthenes();
global $primeUpto;
$low = 1;
$high = $N;
$ans = 0;
while ($low <= $high)
{
$mid = ($low + $high) >> 1;
// Check if the number is
// valid, try to reduce it
if ($mid - $primeUpto[$mid] >= $K)
{
$ans = $mid;
$high = $mid - 1;
}
else
$low = $mid + 1;
}
// ans is the minimum valid number
return ($ans ? $N - $ans + 1 : 0);
}
// Driver Code
$N = 10;
$K = 3;
echo countOfNumbers($N, $K);
// This code is contributed by mits
?>
JavaScript
<script>
// Javascript implementation of the above approach
var MAX = 1000001;
// primeUpto[i] denotes count of prime
// numbers upto i
var primeUpto = Array(MAX).fill(0);
// Function to compute all prime numbers
// and update primeUpto array
function SieveOfEratosthenes()
{
var isPrime = Array(MAX).fill(1);
// 0 and 1 are not primes
isPrime[0] = isPrime[1] = 0;
for (var i = 2; i * i < MAX; i++) {
// If i is prime
if (isPrime[i]) {
// Set all multiples of i as non-prime
for (var j = i * 2; j < MAX; j += i)
isPrime[j] = 0;
}
}
// Compute primeUpto array
for (var i = 1; i < MAX; i++) {
primeUpto[i] = primeUpto[i - 1];
if (isPrime[i])
primeUpto[i]++;
}
}
// Function to return the count
// of valid numbers
function countOfNumbers(N, K)
{
// Compute primeUpto array
SieveOfEratosthenes();
var low = 1, high = N, ans = 0;
while (low <= high) {
var mid = (low + high) >> 1;
// Check if the number is
// valid, try to reduce it
if (mid - primeUpto[mid] >= K) {
ans = mid;
high = mid - 1;
}
else
low = mid + 1;
}
// ans is the minimum valid number
return (ans ? N - ans + 1 : 0);
}
// Driver Code
var N = 10, K = 3;
document.write( countOfNumbers(N, K));
</script>
Time Complexity: O(MAX*log(log(MAX)))
Auxiliary Space: O(MAX)
Similar Reads
Count of numbers whose difference with Fibonacci count upto them is atleast K Prerequisites: Binary SearchGiven two positive integers N and K, the task is to count all the numbers that satisfy the following conditions: If the number is num, num ? N.abs(num - count) ? K where count is the count of fibonacci numbers upto num. Examples: Input: N = 10, K = 3 Output: 2 Explanation
9 min read
Count numbers up to N whose GCD with N is less than that number Given an integer N, the task is to count the values of K ( where 1 ? K? N ), such that 1< GCD(K, N) < K. Examples: Input: N = 10Output: 3Explanation: The values of K which satisfies the given conditions are: K = 4, gcd(4, 10) = 2K = 6, gcd(6, 10) = 2K = 8, gcd(8, 10) = 2 Input: N = 15Output: 4
12 min read
Count of numbers below N whose sum of prime divisors is K Given two integers K and N, the task is to find the count of integers from the range [2, N - 1] whose sum of prime divisors is KExample: Input: N = 20, K = 7 Output: 2 7 and 10 are the only valid numbers. sumPFactors(7) = 7 sumPFactors(10) = 2 + 5 = 7Input: N = 25, K = 5 Output: 5 Approach: Create a
5 min read
Count prime pairs whose difference is also a Prime Number Given an integer N, the task is to count the number of pairs of prime numbers in the range [1, N] such that the difference between elements of each pair is also a prime number. Examples: Input: N = 5 Output: 2 Explanations: Pair of prime numbers in the range [1, 5] whose difference between elements
9 min read
Smallest Semi-Prime Number with at least N difference between any two of its divisors Given a positive integer N, the task is to find the smallest semi-prime number such that the difference between any of its two divisors is at least N. Examples: Input: N = 2Output: 15Explanation:The divisors of 15 are 1, 3, 5, and 15 and the difference between any of its two divisors is greater than
7 min read