omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / fusc-sequence.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text sp " " |
| 8 | text first "first 61 terms:" |
| 9 | text rec "first term of each decimal length, below index 1000000:\n" |
| 10 | text fu "fusc(" |
| 11 | text eqs ") = " |
| 12 | note fu n is fusc(n) for n below 10^6 |
| 13 | global fs ulong 1000000 |
| 14 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 15 | fn emit a view uchar * to void |
| 16 | o = ulong 0 |
| 17 | while lt o len a bound 4096 |
| 18 | k = write 1 a o sub len a o |
| 19 | if le k 0 |
| 20 | exit 1 |
| 21 | end |
| 22 | o = add o ulong k |
| 23 | end |
| 24 | end |
| 25 | note emitu writes n in decimal; 20 digits hold every ulong |
| 26 | fn emitu n ulong to void |
| 27 | d = new uchar 20 0 |
| 28 | i = ulong 20 |
| 29 | v = n |
| 30 | while true bound 20 |
| 31 | i = sub i 1 |
| 32 | put d i uchar add rem v 10 48 |
| 33 | v = div v 10 |
| 34 | if eq v 0 |
| 35 | break |
| 36 | end |
| 37 | end |
| 38 | emit part d i sub 20 i |
| 39 | end |
| 40 | fn digits n ulong to ulong |
| 41 | k = ulong 1 |
| 42 | v = div n 10 |
| 43 | while gt v 0 bound 20 |
| 44 | k = add k 1 |
| 45 | v = div v 10 |
| 46 | end |
| 47 | ret k |
| 48 | end |
| 49 | note fusc(0) = 0, fusc(1) = 1, fusc(2n) = fusc(n), fusc(2n+1) = fusc(n) + fusc(n+1); each needs only smaller indices |
| 50 | fn main to int |
| 51 | put fs 1 1 |
| 52 | for i ulong 999998 |
| 53 | n = add i 2 |
| 54 | h = shr n 1 |
| 55 | if eq rem n 2 0 |
| 56 | put fs n get fs h |
| 57 | else |
| 58 | put fs n add get fs h get fs add h 1 |
| 59 | end |
| 60 | end |
| 61 | emit first |
| 62 | for i ulong 61 |
| 63 | emit sp |
| 64 | emitu get fs i |
| 65 | end |
| 66 | emit nl |
| 67 | emit rec |
| 68 | w = ulong 0 |
| 69 | for n ulong 1000000 |
| 70 | d = digits get fs n |
| 71 | if gt d w |
| 72 | w = d |
| 73 | emit fu |
| 74 | emitu n |
| 75 | emit eqs |
| 76 | emitu get fs n |
| 77 | emit nl |
| 78 | end |
| 79 | end |
| 80 | ret 0 |
| 81 | end |
| 82 | </syntaxhighlight> |
| 83 | {{out}} |
| 84 | <pre> |
| 85 | first 61 terms: 0 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4 1 5 4 7 3 8 5 7 2 7 5 8 3 7 4 5 1 6 5 9 4 11 7 10 3 11 8 13 5 12 7 9 2 9 7 12 5 13 8 11 3 10 7 11 4 |
| 86 | first term of each decimal length, below index 1000000: |
| 87 | fusc(0) = 0 |
| 88 | fusc(37) = 11 |
| 89 | fusc(1173) = 108 |
| 90 | fusc(35499) = 1076 |
| 91 | fusc(699051) = 10946 |
| 92 | </pre> |