Docs


Problem 3: Largest Prime Factor

Problem

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143?

Solution

/*
 * Largest Prime Factor
 * Problem 3 <https://projecteuler.net/problem=3>
 *
 * Project Euler
 *
 * By Ankit R Gadiya
 */

#include <stdio.h>

long int largest_factor (long int num, int fact);

int main (void)
{
	long int num = 600851475143;

	printf("Largest Factor: %ld\n", largest_factor(num, 2));

	return 0;
}

long int largest_factor (long int num, int fact)
{
	if (num == 1) {
		return fact;
	} else {
		while (fact * fact <= num) {
			if (num % fact == 0)
			return largest_factor (num / fact, fact);
			else
			fact++;
		}
		return num;
	}
}

prob3.c

Result

6857