2020-06-05 19:20:25 +03:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief [Problem 3](https://projecteuler.net/problem=3) solution
|
|
|
|
*
|
|
|
|
* Problem:
|
|
|
|
*
|
|
|
|
* The prime factors of 13195 are 5,7,13 and 29. What is the largest prime
|
|
|
|
* factor of a given number N? e.g. for 10, largest prime factor = 5. For 17,
|
|
|
|
* largest prime factor = 17.
|
|
|
|
*/
|
2017-12-04 09:27:24 +03:00
|
|
|
#include <math.h>
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <stdio.h>
|
2017-12-04 09:27:24 +03:00
|
|
|
|
2020-06-05 19:20:25 +03:00
|
|
|
/** Check if the given number is prime */
|
|
|
|
char isprime(int no)
|
2020-04-08 16:41:12 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
int sq;
|
2017-12-04 09:27:24 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
if (no == 2)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (no % 2 == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sq = ((int)(sqrt(no))) + 1;
|
|
|
|
for (int i = 3; i < sq; i += 2)
|
|
|
|
{
|
|
|
|
if (no % i == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2017-12-04 09:27:24 +03:00
|
|
|
}
|
|
|
|
|
2020-06-05 19:20:25 +03:00
|
|
|
/** Main function */
|
2020-04-08 16:41:12 +03:00
|
|
|
int main()
|
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
int maxNumber = 0;
|
|
|
|
int n = 0;
|
|
|
|
int n1;
|
|
|
|
scanf("%d", &n);
|
|
|
|
if (isprime(n) == 1)
|
|
|
|
printf("%d", n);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (n % 2 == 0)
|
|
|
|
{
|
|
|
|
n = n / 2;
|
|
|
|
}
|
|
|
|
if (isprime(n) == 1)
|
|
|
|
{
|
|
|
|
printf("%d\n", n);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
n1 = ((int)(sqrt(n))) + 1;
|
|
|
|
for (int i = 3; i < n1; i += 2)
|
|
|
|
{
|
|
|
|
if (n % i == 0)
|
|
|
|
{
|
|
|
|
if (isprime((int)(n / i)) == 1)
|
|
|
|
{
|
|
|
|
maxNumber = n / i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (isprime(i) == 1)
|
|
|
|
{
|
|
|
|
maxNumber = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("%d\n", maxNumber);
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 19:20:25 +03:00
|
|
|
return 0;
|
2017-12-04 09:27:24 +03:00
|
|
|
}
|