interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / hofstadter-figure-figure-sequences / hofstadter-figure-figure-sequences.om

102 lines · 1925 bytes Raw Blame History
1note Rosetta Code task "Hofstadter Figure-Figure sequences": https://rosettacode.org/wiki/Hofstadter_Figure-Figure_sequences
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text first "R(1) to R(10):"
7text chk "first 40 R and first 960 S cover 1..1000 exactly once: "
8text yes "yes\n"
9text no "no\n"
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36note R(1) = 1, S(1) = 2, R(n) = R(n-1) + S(n-1), S(n) = the least integer above S(n-1) not in R
37note r holds R(1..50) (R(50) = 1509 > 1000, so every R value up to 1000 is marked in ir); s holds S(1..960)
38fn main to int
39r = new ulong 51 0
40s = new ulong 961 0
41ir = new uchar 1600 0
42put r 1 1
43put s 1 2
44put ir 1 1
45for i ulong 49
46n = add i 2
47v = add get r sub n 1 get s sub n 1
48put r n v
49put ir v 1
50c = add get s sub n 1 1
51while eq get ir c 1 bound 2
52c = add c 1
53end
54put s n c
55end
56note S as the complement of R, now that R is known through 1509: the k-th unmarked integer is S(k)
57k = ulong 0
58c = ulong 1
59while lt k 960 bound 1600
60if eq get ir c 0
61k = add k 1
62put s k c
63end
64c = add c 1
65end
66emit first
67for i ulong 10
68emit sp
69emitu get r add i 1
70end
71emit nl
72cnt = new ulong 1001 0
73ok = ulong 1
74for i ulong 40
75v = get r add i 1
76if gt v 1000
77ok = ulong 0
78else
79put cnt v add get cnt v 1
80end
81end
82for i ulong 960
83v = get s add i 1
84if gt v 1000
85ok = ulong 0
86else
87put cnt v add get cnt v 1
88end
89end
90for i ulong 1000
91if ne get cnt add i 1 1
92ok = ulong 0
93end
94end
95emit chk
96if eq ok 1
97emit yes
98else
99emit no
100end
101ret 0
102end