Skip to content

Instantly share code, notes, and snippets.

@antila
Forked from fredrikwarngard/largestPrime.c
Last active August 29, 2015 14:15
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 antila/65250c8d60158ba2207b to your computer and use it in GitHub Desktop.
Save antila/65250c8d60158ba2207b to your computer and use it in GitHub Desktop.
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <inttypes.h>
int64_t divide_into_largest_factor(int64_t input) {
int64_t divisor = 2;
if (2 < input) {
while (divisor < (sqrt(input)+1)) {
if (input % divisor == 0) {
return (input / divisor);
} else {
++divisor;
}
}
}
return input;
}
int main() {
int64_t input = 600851475143;
int64_t temp = input;
int64_t lastTemp = 0;
while (temp != lastTemp) {
lastTemp = temp;
temp = divide_into_largest_factor(temp);
}
printf("outta input %lld; largest prime is: %lld\n", input, temp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment