interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

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

87 lines · 1514 bytes Raw Blame History
1note Rosetta Code task "Hailstone sequence": https://rosettacode.org/wiki/Hailstone_sequence
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text t1 "hailstone(27) has "
7text t2 " elements:"
8text dots " ..."
9text t3 "below 100000 the longest is "
10text t4 " with "
11text t5 " elements\n"
12global seq ulong 1000
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 hfill stores the sequence from n in seq and returns its length; every start below 100000
40note reaches 1 in at most 351 elements, so 999 steps is a proof-backed bound (more traps 4)
41fn hfill n ulong to ulong
42c = ulong 1
43v = n
44put seq 0 v
45while ne v 1 bound 999
46if eq rem v 2 0
47v = div v 2
48else
49v = add mul v 3 1
50end
51put seq c v
52c = add c 1
53end
54ret c
55end
56fn main to int
57c = hfill 27
58emit t1
59emitu c
60emit t2
61for i ulong 4
62emit sp
63emitu get seq i
64end
65emit dots
66for i ulong 4
67emit sp
68emitu get seq add sub c 4 i
69end
70emit nl
71best = ulong 1
72bl = ulong 1
73for i ulong 99999
74n = add i 1
75l = hfill n
76if gt l bl
77bl = l
78best = n
79end
80end
81emit t3
82emitu best
83emit t4
84emitu bl
85emit t5
86ret 0
87end