interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / recamans-sequence / recamans-sequence.om

83 lines · 1532 bytes Raw Blame History
1note Rosetta Code task "Recaman's sequence": https://rosettacode.org/wiki/Recaman%27s_sequence
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text first "first 15 terms:"
7text dup "first term already seen: a("
8text eqs ") = "
9text cov "terms a(0)..a(n) first contain every integer 0..1000 at n = "
10note sn v is 1 once v has appeared; the oracle's run shows no term before 0..1000 is covered exceeds 1942300
11global sn uchar 1942301
12note emit writes every byte of a to stdout; a short write is retried, an error exits 1
13fn emit a view uchar * to void
14o = ulong 0
15while lt o len a bound 4096
16k = write 1 a o sub len a o
17if le k 0
18exit 1
19end
20o = add o ulong k
21end
22end
23note emitu writes n in decimal; 20 digits hold every ulong
24fn emitu n ulong to void
25d = new uchar 20 0
26i = ulong 20
27v = n
28while true bound 20
29i = sub i 1
30put d i uchar add rem v 10 48
31v = div v 10
32if eq v 0
33break
34end
35end
36emit part d i sub 20 i
37end
38note a(0) = 0; a(n) = a(n-1) - n if that is positive and new, else a(n-1) + n
39note bound: the cover is complete at n = 328002, below the 999999 loop bound
40fn main to int
41emit first
42emit sp
43emitu 0
44put sn 0 1
45a = ulong 0
46n = ulong 0
47d = ulong 0
48c = ulong 1
49while lt c 1001 bound 999999
50n = add n 1
51nw = add a n
52if gt a n
53if eq get sn sub a n 0
54nw = sub a n
55end
56end
57a = nw
58if eq get sn a 0
59if le a 1000
60c = add c 1
61end
62else
63if eq d 0
64d = n
65emit nl
66emit dup
67emitu n
68emit eqs
69emitu a
70emit nl
71end
72end
73put sn a 1
74if lt n 15
75emit sp
76emitu a
77end
78end
79emit cov
80emitu n
81emit nl
82ret 0
83end