omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / stern-brocot-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 minus "-" |
| 9 | text tfirst "first 15: " |
| 10 | text tat " first appears at " |
| 11 | text tgcd "gcd of each consecutive pair of the first 1000 is 1: " |
| 12 | text tyes "yes" |
| 13 | text tno "no" |
| 14 | global sb ulong 1200 |
| 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 start 1, 1; for each k from 1 on, append sb[k] + sb[k-1] and then sb[k], until 1200 members |
| 42 | fn build to void |
| 43 | put sb 0 1 |
| 44 | put sb 1 1 |
| 45 | n = ulong 2 |
| 46 | k = ulong 1 |
| 47 | note each pass adds two members, so 600 passes fill 1200 |
| 48 | while lt n 1200 bound 600 |
| 49 | put sb n add get sb k get sb sub k 1 |
| 50 | put sb add n 1 get sb k |
| 51 | n = add n 2 |
| 52 | k = add k 1 |
| 53 | end |
| 54 | end |
| 55 | note find gives the 1-based position of v's first appearance, 0 if not within the 1200 |
| 56 | fn find v ulong to ulong |
| 57 | for i ulong 1200 |
| 58 | if eq get sb i v |
| 59 | ret add i 1 |
| 60 | end |
| 61 | end |
| 62 | ret 0 |
| 63 | end |
| 64 | fn gcd a ulong b ulong to ulong |
| 65 | note Euclid's remainders shrink at least as fast as Fibonacci numbers, so 100 steps cover any ulong |
| 66 | while ne b 0 bound 100 |
| 67 | t = rem a b |
| 68 | a = b |
| 69 | b = t |
| 70 | end |
| 71 | ret a |
| 72 | end |
| 73 | fn show v ulong to void |
| 74 | emitu v |
| 75 | emit tat |
| 76 | emitu find v |
| 77 | emit nl |
| 78 | end |
| 79 | fn main to int |
| 80 | build |
| 81 | emit tfirst |
| 82 | for i ulong 15 |
| 83 | if gt i 0 |
| 84 | emit sp |
| 85 | end |
| 86 | emitu get sb i |
| 87 | end |
| 88 | emit nl |
| 89 | for i ulong 10 |
| 90 | show add i 1 |
| 91 | end |
| 92 | show 100 |
| 93 | ok = true |
| 94 | for i ulong 999 |
| 95 | if ne gcd get sb i get sb add i 1 1 |
| 96 | ok = false |
| 97 | end |
| 98 | end |
| 99 | emit tgcd |
| 100 | if ok |
| 101 | emit tyes |
| 102 | else |
| 103 | emit tno |
| 104 | end |
| 105 | emit nl |
| 106 | ret 0 |
| 107 | end |
| 108 | </syntaxhighlight> |
| 109 | {{out}} |
| 110 | <pre> |
| 111 | first 15: 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4 |
| 112 | 1 first appears at 1 |
| 113 | 2 first appears at 3 |
| 114 | 3 first appears at 5 |
| 115 | 4 first appears at 9 |
| 116 | 5 first appears at 11 |
| 117 | 6 first appears at 33 |
| 118 | 7 first appears at 19 |
| 119 | 8 first appears at 21 |
| 120 | 9 first appears at 35 |
| 121 | 10 first appears at 39 |
| 122 | 100 first appears at 1179 |
| 123 | gcd of each consecutive pair of the first 1000 is 1: yes |
| 124 | </pre> |