|
| 1 | ++++ |
| 2 | +title = "Blum integer" |
| 3 | ++++ |
| 4 | + |
| 5 | +# Blum integer |
| 6 | + |
| 7 | +{{trans|Wren}} |
| 8 | + |
| 9 | +```zc |
| 10 | +import "locale.h" |
| 11 | +
|
| 12 | +let inc = [4, 2, 4, 2, 4, 6, 2, 6]; |
| 13 | +
|
| 14 | +fn is_prime(n: int) -> bool { |
| 15 | + if n < 2 { return false; } |
| 16 | + if n % 2 == 0 { return n == 2; } |
| 17 | + if n % 3 == 0 { return n == 3; } |
| 18 | + let d = 5; |
| 19 | + while d * d <= n { |
| 20 | + if n % d == 0 { return false; } |
| 21 | + d += 2; |
| 22 | + if n % d == 0 { return false; } |
| 23 | + d += 4; |
| 24 | + } |
| 25 | + return true; |
| 26 | +} |
| 27 | +
|
| 28 | +// Assumes n is odd. |
| 29 | +fn first_prime_factor(n: int) -> int { |
| 30 | + if n == 1 { return 1; } |
| 31 | + if n % 3 == 0 { return 3; } |
| 32 | + if n % 5 == 0 { return 5; } |
| 33 | + let k = 7; |
| 34 | + let i = 0; |
| 35 | + while k * k <= n { |
| 36 | + if n % k == 0 { |
| 37 | + return k; |
| 38 | + } else { |
| 39 | + k += inc[i]; |
| 40 | + i = (i + 1) % 8; |
| 41 | + } |
| 42 | + } |
| 43 | + return n; |
| 44 | +} |
| 45 | +
|
| 46 | +fn main() { |
| 47 | + setlocale(LC_NUMERIC, ""); |
| 48 | + let blum: [int; 50]; |
| 49 | + let bc = 0; |
| 50 | + let counts: [int; 10]; |
| 51 | + let digits = [1, 3, 7, 9]; |
| 52 | + let i = 1; |
| 53 | + loop { |
| 54 | + let p = first_prime_factor(i); |
| 55 | + if p % 4 == 3 { |
| 56 | + let q = i / p; |
| 57 | + if q != p && q % 4 == 3 && is_prime(q) { |
| 58 | + if bc < 50 { blum[bc] = i; } |
| 59 | + counts[i % 10]++; |
| 60 | + bc++; |
| 61 | + if bc == 50 { |
| 62 | + println "First 50 Blum integers:"; |
| 63 | + for j in 0..50 { |
| 64 | + print "{blum[j]:3d} "; |
| 65 | + if !((j + 1) % 10) { println ""; } |
| 66 | + } |
| 67 | + println ""; |
| 68 | + } else if bc == 26_828 || bc % 100_000 == 0 { |
| 69 | + println "The {bc:'7d}th Blum integer is: {i:'9d}"; |
| 70 | + if bc == 400_000 { |
| 71 | + println "\n% distribution of the first 400,000 Blum integers:"; |
| 72 | + for j in digits { |
| 73 | + println " {counts[j] / 4000.0:6.3f}% end in {j}"; |
| 74 | + } |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + i = (i % 5 == 3) ? i + 4 : i + 2; |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +**Output:** |
| 86 | + |
| 87 | +``` |
| 88 | +First 50 Blum integers: |
| 89 | + 21 33 57 69 77 93 129 133 141 161 |
| 90 | +177 201 209 213 217 237 249 253 301 309 |
| 91 | +321 329 341 381 393 413 417 437 453 469 |
| 92 | +473 489 497 501 517 537 553 573 581 589 |
| 93 | +597 633 649 669 681 713 717 721 737 749 |
| 94 | +
|
| 95 | +The 26,828th Blum integer is: 524,273 |
| 96 | +The 100,000th Blum integer is: 2,075,217 |
| 97 | +The 200,000th Blum integer is: 4,275,533 |
| 98 | +The 300,000th Blum integer is: 6,521,629 |
| 99 | +The 400,000th Blum integer is: 8,802,377 |
| 100 | +
|
| 101 | +% distribution of the first 400,000 Blum integers: |
| 102 | + 25.001% end in 1 |
| 103 | + 25.017% end in 3 |
| 104 | + 24.997% end in 7 |
| 105 | + 24.985% end in 9 |
| 106 | +``` |
| 107 | + |
| 108 | +--- |
| 109 | +**Attribution:** This is a community solution for the Rosetta Code task [**Blum integer**](https://rosettacode.org/wiki/Blum_integer) in Zen C. |
| 110 | + |
| 111 | +*This article uses material from the Rosetta Code article **Blum integer**, which is released under the [GNU Free Documentation License 1.3](https://www.gnu.org/licenses/fdl-1.3.html). A list of the original authors can be found in the [page history](https://rosettacode.org/wiki/Blum_integer?action=history).* |
0 commit comments