We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I'm new to this website. I attempted this one by declaring an array with the first primes in order such that product of those primes are less than 10^18 and multiplying the next prime would have taken the product higher.
Then simply did a while loop on n calculating the cumulative product and adding one to the index until the product was over n. then return one less than the index. It worked and I didn't have to add any lines but I don't know if this was the most efficient way or if I was not supposed to use consts.
const uint8_t P[]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};
int primeCount(long n) {
uint8_t i=0;
long long prod=1;
while ((prod<=n)&&(i<16)){
prod=(long long)prod*(long long)P[i];
i++;
}
return (i-1);
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Leonardo's Prime Factors
You are viewing a single comment's thread. Return to all comments →
I'm new to this website. I attempted this one by declaring an array with the first primes in order such that product of those primes are less than 10^18 and multiplying the next prime would have taken the product higher. Then simply did a while loop on n calculating the cumulative product and adding one to the index until the product was over n. then return one less than the index. It worked and I didn't have to add any lines but I don't know if this was the most efficient way or if I was not supposed to use consts. const uint8_t P[]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};
int primeCount(long n) { uint8_t i=0; long long prod=1; while ((prod<=n)&&(i<16)){ prod=(long long)prod*(long long)P[i]; i++; }
}