Blum integer: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: use a different loop variable.) |
(Added C) |
||
Line 24: | Line 24: | ||
* OEIS sequence [[oeis:A016105|A016105: Blum integers]] |
* OEIS sequence [[oeis:A016105|A016105: Blum integers]] |
||
<br> |
<br> |
||
=={{header|C}}== |
|||
{{trans|Wren}} |
|||
<syntaxhighlight lang="c">#include <stdio.h> |
|||
#include <stdbool.h> |
|||
#include <locale.h> |
|||
bool isPrime(int n) { |
|||
if (n < 2) return false; |
|||
if (n%2 == 0) return n == 2; |
|||
if (n%3 == 0) return n == 3; |
|||
int d = 5; |
|||
while (d*d <= n) { |
|||
if (n%d == 0) return false; |
|||
d += 2; |
|||
if (n%d == 0) return false; |
|||
d += 4; |
|||
} |
|||
return true; |
|||
} |
|||
void primeFactors(int n, int *factors, int *length) { |
|||
if (n < 2) return; |
|||
int count = 0; |
|||
int inc[8] = {4, 2, 4, 2, 4, 6, 2, 6}; |
|||
while (!(n%2)) { |
|||
factors[count++] = 2; |
|||
n /= 2; |
|||
} |
|||
while (!(n%3)) { |
|||
factors[count++] = 3; |
|||
n /= 3; |
|||
} |
|||
while (!(n%5)) { |
|||
factors[count++] = 5; |
|||
n /= 5; |
|||
} |
|||
for (int k = 7, i = 0; k*k <= n; ) { |
|||
if (!(n%k)) { |
|||
factors[count++] = k; |
|||
n /= k; |
|||
} else { |
|||
k += inc[i]; |
|||
i = (i + 1) % 8; |
|||
} |
|||
} |
|||
if (n > 1) { |
|||
factors[count++] = n; |
|||
} |
|||
*length = count; |
|||
} |
|||
int main() { |
|||
int i = 1, j, bc = 0, length = 0; |
|||
int f[20], blum[50], counts[4] = {0}, digits[4] = {1, 3, 5, 7}; |
|||
setlocale(LC_NUMERIC, ""); |
|||
while (true) { |
|||
primeFactors(i, f, &length); |
|||
if (length == 2 && f[0] != f[1] && f[0] % 4 == 3 && f[1] % 4 == 3) { |
|||
if (bc < 50) blum[bc] = i; |
|||
++counts[i % 10 / 3]; |
|||
++bc; |
|||
if (bc == 50) { |
|||
printf("First 50 Blum integers:\n"); |
|||
for (j = 0; j < 50; ++j) { |
|||
printf("%3d ", blum[j]); |
|||
if (!((j+1) % 10)) printf("\n"); |
|||
} |
|||
printf("\n"); |
|||
} else if (bc == 26828 || !(bc % 100000)) { |
|||
printf("The %'7dth Blum integer is: %'9d\n", bc, i); |
|||
if (bc == 400000) { |
|||
printf("\n%% distribution of the first 400,000 Blum integers:\n"); |
|||
for (j = 0; j < 4; ++j) { |
|||
printf(" %6.3f%% end in %d\n", counts[j]/4000.0, digits[j]); |
|||
} |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
i = (i % 5 == 3) ? i + 4 : i + 2; |
|||
} |
|||
return 0; |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Same as Wren example. |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |