Created
November 23, 2018 05:25
-
-
Save Yepoleb/d12b010ffad7ebf837305be186989058 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
#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