📜  计数数字<= N,其与素数计数的差为> = K

📅  最后修改于: 2021-05-04 22:36:48             🧑  作者: Mango

给定两个正整数NK ,任务是计算满足以下条件的所有数字:
如果数字是num

  • num≤N
  • abs(num – count)≥K ,其中count是最大为num的素数的计数。

例子:

先决条件:二进制搜索

方法:观察到一个函数,它是质数的数量与质数的差直到该数字,对于一个特定的K而言,是一个单调递增的函数。同样,如果数字X是有效数字,则X +1也是有效数字。
证明 :

因此,我们可以使用二进制搜索来找到最小有效数字X,并且从XN的所有数字都是有效数字。因此,答案将是N – X + 1

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
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
> 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 
?>


输出:
5