Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues
Created July 18, 2016 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CraigRodrigues/f6b4a0e310666ab026c97b5fd173b84e to your computer and use it in GitHub Desktop.
Save CraigRodrigues/f6b4a0e310666ab026c97b5fd173b84e to your computer and use it in GitHub Desktop.
Project Euler - Problem 3
#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