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/sum-digits-of-an-integer/sum-digits-of-an-integer.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 / sum-digits-of-an-integer / sum-digits-of-an-integer.om` 82 lines ยท 1494 bytes Raw Blame History 1 note Rosetta Code task "Sum digits of an integer": https://rosettacode.org/wiki/Sum_digits_of_an_integer 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text dig "0123456789abcdefghijklmnopqrstuvwxyz" 6 text bs " (base " 7 text eqs "): digit sum " 8 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 9 fn emit a view uchar * to void 10 o = ulong 0 11 while lt o len a bound 4096 12 k = write 1 a o sub len a o 13 if le k 0 14 exit 1 15 end 16 o = add o ulong k 17 end 18 end 19 note emitu writes n in decimal; 20 digits hold every ulong 20 fn emitu n ulong to void 21 d = new uchar 20 0 22 i = ulong 20 23 v = n 24 while true bound 20 25 i = sub i 1 26 put d i uchar add rem v 10 48 27 v = div v 10 28 if eq v 0 29 break 30 end 31 end 32 emit part d i sub 20 i 33 end 34 note emitc writes the single byte c 35 fn emitc c uchar to void 36 b = new uchar 1 c 37 emit b 38 end 39 note emitb writes n in base b (2..36); a ulong has at most 64 digits in base 2 40 fn emitb n ulong b ulong to void 41 buf = new uchar 64 0 42 k = ulong 0 43 v = n 44 while true bound 64 45 put buf k get dig rem v b 46 k = add k 1 47 v = div v b 48 if eq v 0 49 break 50 end 51 end 52 for i ulong k 53 emitc get buf sub sub k 1 i 54 end 55 end 56 fn dsum n ulong b ulong to ulong 57 s = ulong 0 58 v = n 59 while gt v 0 bound 64 60 s = add s rem v b 61 v = div v b 62 end 63 ret s 64 end 65 fn tell n ulong b ulong to void 66 emitb n b 67 emit bs 68 emitu b 69 emit eqs 70 emitu dsum n b 71 emit nl 72 end 73 fn main to int 74 tell 1 10 75 tell 1234 10 76 tell 0xfe 16 77 tell 0xf0e 16 78 tell 255 8 79 tell `18446744073709551615` 2 80 tell `18446744073709551615` 36 81 ret 0 82 end