Leonardo’s Prime Factors- Hackerrank Problem (C++)
https://www.hackerrank.com/challenges/leonardo-and-prime/problem?isFullScreen=true
Leonardo’s Prime Factors- Hackerrank Problem (C++)
https://www.hackerrank.com/challenges/leonardo-and-prime/problem?isFullScreen=true
Leonardo loves primes and created queries where each query takes the form of an integer, . For each , 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 and itself, and 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 . Another is .
Function Description
Complete the primeCount function in the editor below.
primeCount has the following parameters:
- int n: the inclusive limit of the range to check
Returns
- int: the maximum number of distinct prime factors of any number in the inclusive range .
Input Format
The first line contains an integer, , the number of queries. Each of the next lines contains a single integer, .
Constraints
Sample Input
6
1
2
3
500
5000
10000000000
Sample Output
0
1
1
4
5
10
Explanation
- is not prime and its only factor is itself.
- has prime factor, .
- The number has prime factor, , has and has prime factors.
- The product of the first four primes is . While higher value primes may be a factor of some numbers, there will never be more than distinct prime factors for a number in this range.
Solution
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
/*
* Complete the 'primeCount' function below.
*
* The function is expected to return an INTEGER.
* The function accepts LONG_INTEGER n as parameter.
*/
int primeCount(long n) {
vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
unsigned long long product = 1;
int count = 0;
if (n == 1)
{
return count;
}
for (int prime : primes)
{
// product of primes
product *= (unsigned long long)prime;
// exit conditions
if (product > n)
break;
if (product == n)
{
count++;
break;
}
count++;
}
return count;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string q_temp;
getline(cin, q_temp);
int q = stoi(ltrim(rtrim(q_temp)));
for (int q_itr = 0; q_itr < q; q_itr++) {
string n_temp;
getline(cin, n_temp);
long n = stol(ltrim(rtrim(n_temp)));
int result = primeCount(n);
fout << result << "\n";
}
fout.close();
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
} 메타데이터
- post_id
- 49bc8e7f0b8a
- slug
- leonardos-prime-factors-hackerrank-problem-c-49bc8e7f0b8a
- url
- https://medium.com/@Emdad/leonardos-prime-factors-hackerrank-problem-c-49bc8e7f0b8a
- canonical_url
- https://medium.com/@Emdad/leonardos-prime-factors-hackerrank-problem-c-49bc8e7f0b8a
- author_url
- https://medium.com/@Emdad
- status
- ok
- fetched_at
- 2026-08-31 01:25:58