omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / hofstadter-figure-figure-sequences / hofstadter-figure-figure-sequences.om
| 1 | note Rosetta Code task "Hofstadter Figure-Figure sequences": https://rosettacode.org/wiki/Hofstadter_Figure-Figure_sequences |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text sp " " |
| 6 | text first "R(1) to R(10):" |
| 7 | text chk "first 40 R and first 960 S cover 1..1000 exactly once: " |
| 8 | text yes "yes\n" |
| 9 | text no "no\n" |
| 10 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 11 | fn emit a view uchar * to void |
| 12 | o = ulong 0 |
| 13 | while lt o len a bound 4096 |
| 14 | k = write 1 a o sub len a o |
| 15 | if le k 0 |
| 16 | exit 1 |
| 17 | end |
| 18 | o = add o ulong k |
| 19 | end |
| 20 | end |
| 21 | note emitu writes n in decimal; 20 digits hold every ulong |
| 22 | fn emitu n ulong to void |
| 23 | d = new uchar 20 0 |
| 24 | i = ulong 20 |
| 25 | v = n |
| 26 | while true bound 20 |
| 27 | i = sub i 1 |
| 28 | put d i uchar add rem v 10 48 |
| 29 | v = div v 10 |
| 30 | if eq v 0 |
| 31 | break |
| 32 | end |
| 33 | end |
| 34 | emit part d i sub 20 i |
| 35 | end |
| 36 | note R(1) = 1, S(1) = 2, R(n) = R(n-1) + S(n-1), S(n) = the least integer above S(n-1) not in R |
| 37 | note r holds R(1..50) (R(50) = 1509 > 1000, so every R value up to 1000 is marked in ir); s holds S(1..960) |
| 38 | fn main to int |
| 39 | r = new ulong 51 0 |
| 40 | s = new ulong 961 0 |
| 41 | ir = new uchar 1600 0 |
| 42 | put r 1 1 |
| 43 | put s 1 2 |
| 44 | put ir 1 1 |
| 45 | for i ulong 49 |
| 46 | n = add i 2 |
| 47 | v = add get r sub n 1 get s sub n 1 |
| 48 | put r n v |
| 49 | put ir v 1 |
| 50 | c = add get s sub n 1 1 |
| 51 | while eq get ir c 1 bound 2 |
| 52 | c = add c 1 |
| 53 | end |
| 54 | put s n c |
| 55 | end |
| 56 | note S as the complement of R, now that R is known through 1509: the k-th unmarked integer is S(k) |
| 57 | k = ulong 0 |
| 58 | c = ulong 1 |
| 59 | while lt k 960 bound 1600 |
| 60 | if eq get ir c 0 |
| 61 | k = add k 1 |
| 62 | put s k c |
| 63 | end |
| 64 | c = add c 1 |
| 65 | end |
| 66 | emit first |
| 67 | for i ulong 10 |
| 68 | emit sp |
| 69 | emitu get r add i 1 |
| 70 | end |
| 71 | emit nl |
| 72 | cnt = new ulong 1001 0 |
| 73 | ok = ulong 1 |
| 74 | for i ulong 40 |
| 75 | v = get r add i 1 |
| 76 | if gt v 1000 |
| 77 | ok = ulong 0 |
| 78 | else |
| 79 | put cnt v add get cnt v 1 |
| 80 | end |
| 81 | end |
| 82 | for i ulong 960 |
| 83 | v = get s add i 1 |
| 84 | if gt v 1000 |
| 85 | ok = ulong 0 |
| 86 | else |
| 87 | put cnt v add get cnt v 1 |
| 88 | end |
| 89 | end |
| 90 | for i ulong 1000 |
| 91 | if ne get cnt add i 1 1 |
| 92 | ok = ulong 0 |
| 93 | end |
| 94 | end |
| 95 | emit chk |
| 96 | if eq ok 1 |
| 97 | emit yes |
| 98 | else |
| 99 | emit no |
| 100 | end |
| 101 | ret 0 |
| 102 | end |