-
-
Save CraigRodrigues/f6b4a0e310666ab026c97b5fd173b84e to your computer and use it in GitHub Desktop.
Project Euler - Problem 3
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
#include <cs50.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
/* | |
Largest Prime Factor | |
The prime factors of 13195 are 5, 7, 13 and 29. | |
What is the largest prime factor of the number 600851475143 ? | |
*/ | |
int largest_factor = 0; | |
void largestprime(long long n); | |
int main(void) | |
{ | |
long long number = 600851475143; | |
largestprime(number); | |
printf("The largest prime factor of %lli is %i\n", number, largest_factor); | |
} | |
void largestprime(long long n) | |
{ | |
int d = 2; //start checking primes at 2 | |
while (n > 1) | |
{ | |
while (n % d == 0) | |
{ | |
largest_factor = d; | |
n /= d; | |
} | |
d++; | |
if (d * d > n) | |
{ | |
if (n > 1) | |
{ | |
largest_factor = n; | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment