Self numbers: Difference between revisions

Added C
m (→‎ten millionth self number: changed the title comment.)
(Added C)
Line 12:
;*[[oeis:A003052|OEIS: A003052 - Self numbers or Colombian numbers]]
;*[[wp:Self_number|Wikipedia: Self numbers]]
 
=={{header|C}}==
{{trans|Go}}
The 'sieve based' version.
 
About 25% faster than Go (using GCC 7.5.0 -O3) mainly due to being able to iterate through the sieve using a pointer.
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
typedef unsigned char bool;
 
#define TRUE 1
#define FALSE 0
#define MILLION 1000000
#define BILLION 1000 * MILLION
#define MAX_COUNT 2*BILLION + 9*9 + 1
 
void sieve(bool *sv) {
int n = 0, s[8], a, b, c, d, e, f, g, h, i, j;
for (a = 0; a < 2; ++a) {
for (b = 0; b < 10; ++b) {
s[0] = a + b;
for (c = 0; c < 10; ++c) {
s[1] = s[0] + c;
for (d = 0; d < 10; ++d) {
s[2] = s[1] + d;
for (e = 0; e < 10; ++e) {
s[3] = s[2] + e;
for (f = 0; f < 10; ++f) {
s[4] = s[3] + f;
for (g = 0; g < 10; ++g) {
s[5] = s[4] + g;
for (h = 0; h < 10; ++h) {
s[6] = s[5] + h;
for (i = 0; i < 10; ++i) {
s[7] = s[6] + i;
for (j = 0; j < 10; ++j) {
sv[s[7] + j+ n++] = TRUE;
}
}
}
}
}
}
}
}
}
}
}
 
int main() {
int i, count = 0;
clock_t begin = clock();
bool *p, *sv = (bool*) calloc(MAX_COUNT, sizeof(bool));
sieve(sv);
for (p = sv; p < sv + MAX_COUNT; ++p) {
if (!*p) {
if (++count <= 50) printf("%ld ", p-sv);
if (count == 100 * MILLION) {
printf("\n\nThe 100 millionth self number is %ld\n", p-sv);
break;
}
}
}
clock_t end = clock();
printf("Took %lf seconds.\n", (double)(end - begin) / CLOCKS_PER_SEC);
return 0;
}
</lang>
 
{{out}}
<pre>
1 3 5 7 9 20 31 42 53 64 75 86 97 108 110 121 132 143 154 165 176 187 198 209 211 222 233 244 255 266 277 288 299 310 312 323 334 345 356 367 378 389 400 411 413 424 435 446 457 468
 
The 100 millionth self number is 1022727208
Took 1.518682 seconds.
</pre>
 
=={{header|C#|CSharp}}==
9,490

edits