omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / fusc-sequence / fusc-sequence.om
| 1 | note Rosetta Code task "Fusc sequence": https://rosettacode.org/wiki/Fusc_sequence |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text sp " " |
| 6 | text first "first 61 terms:" |
| 7 | text rec "first term of each decimal length, below index 1000000:\n" |
| 8 | text fu "fusc(" |
| 9 | text eqs ") = " |
| 10 | note fu n is fusc(n) for n below 10^6 |
| 11 | global fs ulong 1000000 |
| 12 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 13 | fn emit a view uchar * to void |
| 14 | o = ulong 0 |
| 15 | while lt o len a bound 4096 |
| 16 | k = write 1 a o sub len a o |
| 17 | if le k 0 |
| 18 | exit 1 |
| 19 | end |
| 20 | o = add o ulong k |
| 21 | end |
| 22 | end |
| 23 | note emitu writes n in decimal; 20 digits hold every ulong |
| 24 | fn emitu n ulong to void |
| 25 | d = new uchar 20 0 |
| 26 | i = ulong 20 |
| 27 | v = n |
| 28 | while true bound 20 |
| 29 | i = sub i 1 |
| 30 | put d i uchar add rem v 10 48 |
| 31 | v = div v 10 |
| 32 | if eq v 0 |
| 33 | break |
| 34 | end |
| 35 | end |
| 36 | emit part d i sub 20 i |
| 37 | end |
| 38 | fn digits n ulong to ulong |
| 39 | k = ulong 1 |
| 40 | v = div n 10 |
| 41 | while gt v 0 bound 20 |
| 42 | k = add k 1 |
| 43 | v = div v 10 |
| 44 | end |
| 45 | ret k |
| 46 | end |
| 47 | note fusc(0) = 0, fusc(1) = 1, fusc(2n) = fusc(n), fusc(2n+1) = fusc(n) + fusc(n+1); each needs only smaller indices |
| 48 | fn main to int |
| 49 | put fs 1 1 |
| 50 | for i ulong 999998 |
| 51 | n = add i 2 |
| 52 | h = shr n 1 |
| 53 | if eq rem n 2 0 |
| 54 | put fs n get fs h |
| 55 | else |
| 56 | put fs n add get fs h get fs add h 1 |
| 57 | end |
| 58 | end |
| 59 | emit first |
| 60 | for i ulong 61 |
| 61 | emit sp |
| 62 | emitu get fs i |
| 63 | end |
| 64 | emit nl |
| 65 | emit rec |
| 66 | w = ulong 0 |
| 67 | for n ulong 1000000 |
| 68 | d = digits get fs n |
| 69 | if gt d w |
| 70 | w = d |
| 71 | emit fu |
| 72 | emitu n |
| 73 | emit eqs |
| 74 | emitu get fs n |
| 75 | emit nl |
| 76 | end |
| 77 | end |
| 78 | ret 0 |
| 79 | end |