Skip to content

Instantly share code, notes, and snippets.

@bojieli
Last active August 29, 2015 14:08
Show Gist options
  • Save bojieli/2d76e32f094334307198 to your computer and use it in GitHub Desktop.
Save bojieli/2d76e32f094334307198 to your computer and use it in GitHub Desktop.
ProjectEuler: Amicable numbers
#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