omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / hailstone-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 t1 "hailstone(27) has " |
| 9 | text t2 " elements:" |
| 10 | text dots " ..." |
| 11 | text t3 "below 100000 the longest is " |
| 12 | text t4 " with " |
| 13 | text t5 " elements\n" |
| 14 | global seq ulong 1000 |
| 15 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 16 | fn emit a view uchar * to void |
| 17 | o = ulong 0 |
| 18 | while lt o len a bound 4096 |
| 19 | k = write 1 a o sub len a o |
| 20 | if le k 0 |
| 21 | exit 1 |
| 22 | end |
| 23 | o = add o ulong k |
| 24 | end |
| 25 | end |
| 26 | note emitu writes n in decimal; 20 digits hold every ulong |
| 27 | fn emitu n ulong to void |
| 28 | d = new uchar 20 0 |
| 29 | i = ulong 20 |
| 30 | v = n |
| 31 | while true bound 20 |
| 32 | i = sub i 1 |
| 33 | put d i uchar add rem v 10 48 |
| 34 | v = div v 10 |
| 35 | if eq v 0 |
| 36 | break |
| 37 | end |
| 38 | end |
| 39 | emit part d i sub 20 i |
| 40 | end |
| 41 | note hfill stores the sequence from n in seq and returns its length; every start below 100000 |
| 42 | note reaches 1 in at most 351 elements, so 999 steps is a proof-backed bound (more traps 4) |
| 43 | fn hfill n ulong to ulong |
| 44 | c = ulong 1 |
| 45 | v = n |
| 46 | put seq 0 v |
| 47 | while ne v 1 bound 999 |
| 48 | if eq rem v 2 0 |
| 49 | v = div v 2 |
| 50 | else |
| 51 | v = add mul v 3 1 |
| 52 | end |
| 53 | put seq c v |
| 54 | c = add c 1 |
| 55 | end |
| 56 | ret c |
| 57 | end |
| 58 | fn main to int |
| 59 | c = hfill 27 |
| 60 | emit t1 |
| 61 | emitu c |
| 62 | emit t2 |
| 63 | for i ulong 4 |
| 64 | emit sp |
| 65 | emitu get seq i |
| 66 | end |
| 67 | emit dots |
| 68 | for i ulong 4 |
| 69 | emit sp |
| 70 | emitu get seq add sub c 4 i |
| 71 | end |
| 72 | emit nl |
| 73 | best = ulong 1 |
| 74 | bl = ulong 1 |
| 75 | for i ulong 99999 |
| 76 | n = add i 1 |
| 77 | l = hfill n |
| 78 | if gt l bl |
| 79 | bl = l |
| 80 | best = n |
| 81 | end |
| 82 | end |
| 83 | emit t3 |
| 84 | emitu best |
| 85 | emit t4 |
| 86 | emitu bl |
| 87 | emit t5 |
| 88 | ret 0 |
| 89 | end |
| 90 | </syntaxhighlight> |
| 91 | {{out}} |
| 92 | <pre> |
| 93 | hailstone(27) has 112 elements: 27 82 41 124 ... 8 4 2 1 |
| 94 | below 100000 the longest is 77031 with 351 elements |
| 95 | </pre> |