Skip to content

Instantly share code, notes, and snippets.

@Yepoleb
Created November 23, 2018 05:25
Show Gist options
  • Save Yepoleb/d12b010ffad7ebf837305be186989058 to your computer and use it in GitHub Desktop.
Save Yepoleb/d12b010ffad7ebf837305be186989058 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
int end = 35000000;
int *sums = malloc(end * sizeof(int));
// Limit where we start counting divisors twice
int max_div = sqrtf(end);
// Initialize all sums with 1
for (int i = 0; i < end; i++) {
sums[i] = 1;
}
for (int div = 2; div <= max_div; div++) {
// Start at the cube to not count divisors twice
int mult = div * div;
int counter = div;
while (mult < end) {
sums[mult] += div;
sums[mult] += counter;
mult += div;
counter++;
}
}
// skip 1
for (int i = 2; i < end; i++) {
if (sums[i] == i) {
printf("%d\n", i);
}
}
free(sums);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment