Leonardo's Prime Factors

Leonardo loves primes and created q queries where each query takes the form of an integer,n. For each n, count the maximum number of distinct prime factors of any number in the inclusive range.

Note: Recall that a prime number is only divisible by 1 and itself, and 1 is not a prime number.

Example

The maximum number of distinct prime factors for values less than or equal to is. One value with distinct prime factors is 30. Another is 42.

Example:

Input:  q = 6, arr = {1, 2, 3, 500, 5000, 10000000000}

Output:

0 1 1 4 5 10

Approach:

C++

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

int primeCount(long long int N)
{
    vector<long> primes = {2, 3, 5, 7, 11, 13,
                           17, 19, 23, 29, 31, 37,
                           41, 43, 47, 53};
    int count = 0;
    long  long int pf = primes[0];
    for (int j = 1; j < primes.size() && pf <= N && N != 1; j++)
    {
        pf = pf * primes[j];
        count++;
    }
    return count;
}

int main()
{
    int q = 6;

    vector<long long int > arr = {1, 2, 3,
                             500, 5000,
                             10000000000};

    for (int i = 0; i < q; i++)
    {

        cout << primeCount(arr[i]) << "\n";
    }

    return 0;
}


No comments:

Post a Comment