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 / least-common-multiple.wiki

98 lines · 1795 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 pre "lcm("
8text cm ", "
9text eqs ") = "
10text tup "lcm(1.."
11note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12fn emit a view uchar * to void
13o = ulong 0
14while lt o len a bound 4096
15k = write 1 a o sub len a o
16if le k 0
17exit 1
18end
19o = add o ulong k
20end
21end
22note emitu writes n in decimal; 20 digits hold every ulong
23fn emitu n ulong to void
24d = new uchar 20 0
25i = ulong 20
26v = n
27while true bound 20
28i = sub i 1
29put d i uchar add rem v 10 48
30v = div v 10
31if eq v 0
32break
33end
34end
35emit part d i sub 20 i
36end
37note Euclid: each two steps at least halve x, so 128 steps cover any 64-bit pair
38fn gcd a ulong b ulong to ulong
39x = a
40y = b
41while ne y 0 bound 128
42t = rem x y
43x = y
44y = t
45end
46ret x
47end
48note lcm(a, b) = a / gcd(a, b) * b, dividing first so the product never exceeds the result
49fn lcm a ulong b ulong to ulong
50if or eq a 0 eq b 0
51ret 0
52end
53ret mul div a gcd a b b
54end
55fn tell a ulong b ulong to void
56emit pre
57emitu a
58emit cm
59emitu b
60emit eqs
61emitu lcm a b
62emit nl
63end
64note lcm of 1..n by folding; lcm(1..40) = 5342931457063200 is below 2^64
65fn upto n ulong to void
66r = ulong 1
67k = ulong 1
68while le k n bound 40
69r = lcm r k
70k = add k 1
71end
72emit tup
73emitu n
74emit eqs
75emitu r
76emit nl
77end
78fn main to int
79tell 12 18
80tell 35 0
81tell 0 5
82tell 4294967296 6000000000
83upto 10
84upto 20
85upto 40
86ret 0
87end
88</syntaxhighlight>
89{{out}}
90<pre>
91lcm(12, 18) = 36
92lcm(35, 0) = 0
93lcm(0, 5) = 0
94lcm(4294967296, 6000000000) = 25165824000000000
95lcm(1..10) = 2520
96lcm(1..20) = 232792560
97lcm(1..40) = 5342931457063200
98</pre>