forked from PawanKolhe/C-Algorithms
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PawanKolhe#2 | Nenad Pantelic | Add third problem solution - largest …
…prime factor
- Loading branch information
1 parent
7aa0d0f
commit 2337fb1
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
/* | ||
Find maximum prime factor of number num | ||
*/ | ||
int isPrime(int x){ | ||
|
||
int i,prime = 1; | ||
for (i = 2; i < x && prime; i++){ | ||
if (!(x%i)) prime = 0; | ||
} | ||
return prime; | ||
} | ||
|
||
int main() | ||
{ | ||
long num = 600851475143; | ||
int max_prime = 0; | ||
|
||
int i = 3; | ||
while (num != 1){ | ||
if (!(num % i) && isPrime(i)){ | ||
num /= i; | ||
if (i > max_prime) max_prime = i; | ||
} | ||
i++; | ||
|
||
} | ||
printf("%d\n",max_prime); | ||
return 0; | ||
} |