omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / hofstadter-q-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 10 terms:" |
| 9 | text th "the 1000th term: " |
| 10 | text fl "terms up to the 100000th that are less than the term before: " |
| 11 | note q n is Q(n) for n = 1..100000; Q(n) < n here, so every index n - Q(n-1) and n - Q(n-2) is in range |
| 12 | global q ulong 100001 |
| 13 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 14 | fn emit a view uchar * to void |
| 15 | o = ulong 0 |
| 16 | while lt o len a bound 4096 |
| 17 | k = write 1 a o sub len a o |
| 18 | if le k 0 |
| 19 | exit 1 |
| 20 | end |
| 21 | o = add o ulong k |
| 22 | end |
| 23 | end |
| 24 | note emitu writes n in decimal; 20 digits hold every ulong |
| 25 | fn emitu n ulong to void |
| 26 | d = new uchar 20 0 |
| 27 | i = ulong 20 |
| 28 | v = n |
| 29 | while true bound 20 |
| 30 | i = sub i 1 |
| 31 | put d i uchar add rem v 10 48 |
| 32 | v = div v 10 |
| 33 | if eq v 0 |
| 34 | break |
| 35 | end |
| 36 | end |
| 37 | emit part d i sub 20 i |
| 38 | end |
| 39 | fn main to int |
| 40 | put q 1 1 |
| 41 | put q 2 1 |
| 42 | for i ulong 99998 |
| 43 | n = add i 3 |
| 44 | a = get q sub n get q sub n 1 |
| 45 | b = get q sub n get q sub n 2 |
| 46 | put q n add a b |
| 47 | end |
| 48 | emit first |
| 49 | for i ulong 10 |
| 50 | emit sp |
| 51 | emitu get q add i 1 |
| 52 | end |
| 53 | emit nl |
| 54 | emit th |
| 55 | emitu get q 1000 |
| 56 | emit nl |
| 57 | c = ulong 0 |
| 58 | for i ulong 99999 |
| 59 | n = add i 2 |
| 60 | if lt get q n get q sub n 1 |
| 61 | c = add c 1 |
| 62 | end |
| 63 | end |
| 64 | emit fl |
| 65 | emitu c |
| 66 | emit nl |
| 67 | ret 0 |
| 68 | end |
| 69 | </syntaxhighlight> |
| 70 | {{out}} |
| 71 | <pre> |
| 72 | first 10 terms: 1 1 2 3 3 4 5 5 6 6 |
| 73 | the 1000th term: 502 |
| 74 | terms up to the 100000th that are less than the term before: 49798 |
| 75 | </pre> |