interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / stern-brocot-sequence.wiki

124 lines · 2367 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text minus "-"
9text tfirst "first 15: "
10text tat " first appears at "
11text tgcd "gcd of each consecutive pair of the first 1000 is 1: "
12text tyes "yes"
13text tno "no"
14global sb ulong 1200
15note emit writes every byte of a to stdout; a short write is retried, an error exits 1
16fn emit a view uchar * to void
17o = ulong 0
18while lt o len a bound 4096
19k = write 1 a o sub len a o
20if le k 0
21exit 1
22end
23o = add o ulong k
24end
25end
26note emitu writes n in decimal; 20 digits hold every ulong
27fn emitu n ulong to void
28d = new uchar 20 0
29i = ulong 20
30v = n
31while true bound 20
32i = sub i 1
33put d i uchar add rem v 10 48
34v = div v 10
35if eq v 0
36break
37end
38end
39emit part d i sub 20 i
40end
41note start 1, 1; for each k from 1 on, append sb[k] + sb[k-1] and then sb[k], until 1200 members
42fn build to void
43put sb 0 1
44put sb 1 1
45n = ulong 2
46k = ulong 1
47note each pass adds two members, so 600 passes fill 1200
48while lt n 1200 bound 600
49put sb n add get sb k get sb sub k 1
50put sb add n 1 get sb k
51n = add n 2
52k = add k 1
53end
54end
55note find gives the 1-based position of v's first appearance, 0 if not within the 1200
56fn find v ulong to ulong
57for i ulong 1200
58if eq get sb i v
59ret add i 1
60end
61end
62ret 0
63end
64fn gcd a ulong b ulong to ulong
65note Euclid's remainders shrink at least as fast as Fibonacci numbers, so 100 steps cover any ulong
66while ne b 0 bound 100
67t = rem a b
68a = b
69b = t
70end
71ret a
72end
73fn show v ulong to void
74emitu v
75emit tat
76emitu find v
77emit nl
78end
79fn main to int
80build
81emit tfirst
82for i ulong 15
83if gt i 0
84emit sp
85end
86emitu get sb i
87end
88emit nl
89for i ulong 10
90show add i 1
91end
92show 100
93ok = true
94for i ulong 999
95if ne gcd get sb i get sb add i 1 1
96ok = false
97end
98end
99emit tgcd
100if ok
101emit tyes
102else
103emit tno
104end
105emit nl
106ret 0
107end
108</syntaxhighlight>
109{{out}}
110<pre>
111first 15: 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4
1121 first appears at 1
1132 first appears at 3
1143 first appears at 5
1154 first appears at 9
1165 first appears at 11
1176 first appears at 33
1187 first appears at 19
1198 first appears at 21
1209 first appears at 35
12110 first appears at 39
122100 first appears at 1179
123gcd of each consecutive pair of the first 1000 is 1: yes
124</pre>