Skip to content

Commit

Permalink
add solution for P21
Browse files Browse the repository at this point in the history
  • Loading branch information
generic-github-user committed Nov 18, 2022
1 parent 11192da commit ae0afb3
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions puzzles/project-euler/21.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>

int d(int x) {
int sum = 0;
printf("%6d : ", x);
// while (x > 1) {
for (int i=1; i<x; i++) {
if (x % i == 0) {
// x /= i;
sum += i;
printf(" %d", i);
}
}
// }
printf(" ~ %d\n", sum);
return sum;
}

int main() {
int n = 10000;
// int test[n];
int S = 0;
for (int i=1; i<n; i++) {
int di = d(i);
if (di < n && i != di && i == d(di)) {
printf("amicable pair: %d, %d\n", i, di);
S += i;
}
}
printf("%d\n", S);
}

0 comments on commit ae0afb3

Please sign in to comment.