Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/tasks/least-common-multiple/least-common-multiple.om # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-rosetta / tasks / least-common-multiple / least-common-multiple.om` 85 lines ยท 1512 bytes Raw Blame History 1 note Rosetta Code task "Least common multiple": https://rosettacode.org/wiki/Least_common_multiple 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text pre "lcm(" 6 text cm ", " 7 text eqs ") = " 8 text tup "lcm(1.." 9 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 10 fn emit a view uchar * to void 11 o = ulong 0 12 while lt o len a bound 4096 13 k = write 1 a o sub len a o 14 if le k 0 15 exit 1 16 end 17 o = add o ulong k 18 end 19 end 20 note emitu writes n in decimal; 20 digits hold every ulong 21 fn emitu n ulong to void 22 d = new uchar 20 0 23 i = ulong 20 24 v = n 25 while true bound 20 26 i = sub i 1 27 put d i uchar add rem v 10 48 28 v = div v 10 29 if eq v 0 30 break 31 end 32 end 33 emit part d i sub 20 i 34 end 35 note Euclid: each two steps at least halve x, so 128 steps cover any 64-bit pair 36 fn gcd a ulong b ulong to ulong 37 x = a 38 y = b 39 while ne y 0 bound 128 40 t = rem x y 41 x = y 42 y = t 43 end 44 ret x 45 end 46 note lcm(a, b) = a / gcd(a, b) * b, dividing first so the product never exceeds the result 47 fn lcm a ulong b ulong to ulong 48 if or eq a 0 eq b 0 49 ret 0 50 end 51 ret mul div a gcd a b b 52 end 53 fn tell a ulong b ulong to void 54 emit pre 55 emitu a 56 emit cm 57 emitu b 58 emit eqs 59 emitu lcm a b 60 emit nl 61 end 62 note lcm of 1..n by folding; lcm(1..40) = `5342931457063200` is below 2^64 63 fn upto n ulong to void 64 r = ulong 1 65 k = ulong 1 66 while le k n bound 40 67 r = lcm r k 68 k = add k 1 69 end 70 emit tup 71 emitu n 72 emit eqs 73 emitu r 74 emit nl 75 end 76 fn main to int 77 tell 12 18 78 tell 35 0 79 tell 0 5 80 tell `4294967296` `6000000000` 81 upto 10 82 upto 20 83 upto 40 84 ret 0 85 end