interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / stern-brocot-sequence / stern-brocot-sequence.om

105 lines · 1909 bytes Raw Blame History
1note Rosetta Code task "Stern-Brocot sequence": https://rosettacode.org/wiki/Stern-Brocot_sequence
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tfirst "first 15: "
8text tat " first appears at "
9text tgcd "gcd of each consecutive pair of the first 1000 is 1: "
10text tyes "yes"
11text tno "no"
12global sb ulong 1200
13note emit writes every byte of a to stdout; a short write is retried, an error exits 1
14fn emit a view uchar * to void
15o = ulong 0
16while lt o len a bound 4096
17k = write 1 a o sub len a o
18if le k 0
19exit 1
20end
21o = add o ulong k
22end
23end
24note emitu writes n in decimal; 20 digits hold every ulong
25fn emitu n ulong to void
26d = new uchar 20 0
27i = ulong 20
28v = n
29while true bound 20
30i = sub i 1
31put d i uchar add rem v 10 48
32v = div v 10
33if eq v 0
34break
35end
36end
37emit part d i sub 20 i
38end
39note start 1, 1; for each k from 1 on, append sb[k] + sb[k-1] and then sb[k], until 1200 members
40fn build to void
41put sb 0 1
42put sb 1 1
43n = ulong 2
44k = ulong 1
45note each pass adds two members, so 600 passes fill 1200
46while lt n 1200 bound 600
47put sb n add get sb k get sb sub k 1
48put sb add n 1 get sb k
49n = add n 2
50k = add k 1
51end
52end
53note find gives the 1-based position of v's first appearance, 0 if not within the 1200
54fn find v ulong to ulong
55for i ulong 1200
56if eq get sb i v
57ret add i 1
58end
59end
60ret 0
61end
62fn gcd a ulong b ulong to ulong
63note Euclid's remainders shrink at least as fast as Fibonacci numbers, so 100 steps cover any ulong
64while ne b 0 bound 100
65t = rem a b
66a = b
67b = t
68end
69ret a
70end
71fn show v ulong to void
72emitu v
73emit tat
74emitu find v
75emit nl
76end
77fn main to int
78build
79emit tfirst
80for i ulong 15
81if gt i 0
82emit sp
83end
84emitu get sb i
85end
86emit nl
87for i ulong 10
88show add i 1
89end
90show 100
91ok = true
92for i ulong 999
93if ne gcd get sb i get sb add i 1 1
94ok = false
95end
96end
97emit tgcd
98if ok
99emit tyes
100else
101emit tno
102end
103emit nl
104ret 0
105end