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.
unsigned long long findLargestPrimeFactor(unsigned long long n);
int isPrime(unsigned long long n);
int main()
{
int test_case = 0;
scanf("%d", &test_case);
while (test_case--)
{
unsigned long long n = 0;
scanf("%llu", &n);
unsigned long long res = findLargestPrimeFactor(n);
printf("%llu\n", res);
}
return 0;
}
unsigned long long findLargestPrimeFactor(unsigned long long n)
{
unsigned long long largest_prime_factor = 1;
if (isPrime(n) == 1)
{
return n;
}
for (unsigned long long factor1 = 2; factor1*factor1 <= n; factor1++) {
if (n % factor1 == 0)
{
unsigned long long factor2 = n / factor1;
if (isPrime(factor2) == 1)
{
return factor2;
}
if (isPrime(factor1) == 1)
{
if (factor1 > largest_prime_factor)
{
largest_prime_factor = factor1;
}
}
}
}
return largest_prime_factor;
}
int isPrime(unsigned long long n)
{
for (unsigned long long i = 2; i*i <= n; i++)
{
if (n % i == 0)
{
return 0;
}
}
return 1;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #3: Largest prime factor
You are viewing a single comment's thread. Return to all comments →
include
include
include
include
include
include
include
unsigned long long findLargestPrimeFactor(unsigned long long n); int isPrime(unsigned long long n);
int main() { int test_case = 0; scanf("%d", &test_case);
}
unsigned long long findLargestPrimeFactor(unsigned long long n) { unsigned long long largest_prime_factor = 1; if (isPrime(n) == 1) { return n; }
}
int isPrime(unsigned long long n) { for (unsigned long long i = 2; i*i <= n; i++) { if (n % i == 0) { return 0; } }
}