-
-
Save bojieli/2d76e32f094334307198 to your computer and use it in GitHub Desktop.
ProjectEuler: Amicable numbers
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<stdio.h> | |
static long get_factor_num(long n) { | |
int factor; | |
int exp_count = 1; | |
int ans = 1; | |
for (factor = 2; n > 1; factor++) { | |
exp_count = 1; | |
while (n % factor == 0) { | |
n /= factor; | |
exp_count *= factor; | |
} | |
ans *= (exp_count * factor - 1) / (factor - 1); | |
} | |
return ans; | |
} | |
int main() { | |
int i, sum = 0; | |
for (i=2; i<10000; i++) { | |
int n = get_factor_num(i) - i; | |
if (n == i) | |
continue; | |
if (get_factor_num(n) - n == i) | |
sum += i; | |
} | |
printf("%d\n", sum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment