Forbidden numbers: Difference between revisions

Added C
(Added Go)
(Added C)
Line 32:
;* [[oeis:A004215|OEIS A004215 - Numbers that are the sum of 4 but no fewer nonzero squares]]
 
 
=={{header|C}}==
A translation of the Python code in the OEIS link. Runtime around 5.6 seconds.
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <locale.h>
 
bool isForbidden(int n) {
int m = n, v = 0, p;
while (m > 1 && !(m % 4)) {
m /= 4;
++v;
}
p = (int)pow(4.0, (double)v);
return n / p % 8 == 7;
}
 
int main() {
int i = 0, count = 0, limit = 500;
printf("The first 50 forbidden numbers are:\n");
for ( ; count < 50; ++i) {
if (isForbidden(i)) {
printf("%3d ", i);
++count;
if (!(count+1)%10) printf("\n");
}
}
printf("\n\n");
setlocale(LC_NUMERIC, "");
for (i = 1, count = 0; ; ++i) {
if (isForbidden(i)) ++count;
if (i == limit) {
printf("Forbidden number count <= %'11d: %'10d\n", limit, count);
if (limit == 500000000) break;
limit *= 10;
}
}
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
The first 50 forbidden numbers are:
7 15 23 28 31 39 47 55 60 63 71 79 87 92 95 103 111 112 119 124 127 135 143 151 156 159 167 175 183 188 191 199 207 215 220 223 231 239 240 247 252 255 263 271 279 284 287 295 303 311
 
Forbidden number count <= 500: 82
Forbidden number count <= 5,000: 831
Forbidden number count <= 50,000: 8,330
Forbidden number count <= 500,000: 83,331
Forbidden number count <= 5,000,000: 833,329
Forbidden number count <= 50,000,000: 8,333,330
Forbidden number count <= 500,000,000: 83,333,328
</pre>
 
=={{header|Go}}==
9,485

edits