Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues CraigRodrigues/problem3.c Secret
Created Jul 18, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.