Micro and Prime Prime

Micro just learned about prime numbers. But he quickly got bored of them, so he defined a new kind of number and called them Prime Prime Numbers. A number X is Prime Prime if a number of prime numbers from 1 to X (inclusive) are prime. Now he wants to find out the number of Prime Prime numbers from L to R (inclusive). Help Micro with it.

Example:

Input:  l = 3, r = 10
Output: 4

Approach

C++

#include <bits/stdc++.h>
using namespace std;

bool dp[100001];
void primeInitialize()
{

    memset(dp, truesizeof(dp));
    dp[0] = false;
    dp[1] = false;
    for (int i = 2; i * i <= 100000; i++)
    {
        if (dp[i])
        {
            for (int j = i * i; j <= 100000; j += i)
                dp[j] = false;
        }
    }
}
int main()
{

    primeInitialize();
    int sum[100001];
    sum[0] = 0;
    sum[1] = 0;
    int cnt = 0;
    for (int i = 2; i <= 100000; i++)
    {
        if (dp[i] == 1)
            cnt++;
        if (dp[cnt] == 1)
            sum[i] = 1;
        else
            sum[i] = 0;
    }

    for (int i = 1; i <= 100000; i++)
        sum[i] += sum[i - 1];

    int l = 3, r = 10;

    int res = sum[r] - sum[l - 1];
    cout << res << "\n";
}


No comments:

Post a Comment