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/drafts/rosettacode/sum-digits-of-an-integer.wiki # 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 / drafts / rosettacode / sum-digits-of-an-integer.wiki` 95 lines ยท 1869 bytes Raw Blame History 1 =={{header|Omegese}}== 2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}} 3 Omega has no imports, so the small output helpers (emit and friends) are part of the program. 4 5 om2 6 text nl "\n" 7 text dig "0123456789abcdefghijklmnopqrstuvwxyz" 8 text bs " (base " 9 text eqs "): digit sum " 10 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 11 fn emit a view uchar * to void 12 o = ulong 0 13 while lt o len a bound 4096 14 k = write 1 a o sub len a o 15 if le k 0 16 exit 1 17 end 18 o = add o ulong k 19 end 20 end 21 note emitu writes n in decimal; 20 digits hold every ulong 22 fn emitu n ulong to void 23 d = new uchar 20 0 24 i = ulong 20 25 v = n 26 while true bound 20 27 i = sub i 1 28 put d i uchar add rem v 10 48 29 v = div v 10 30 if eq v 0 31 break 32 end 33 end 34 emit part d i sub 20 i 35 end 36 note emitc writes the single byte c 37 fn emitc c uchar to void 38 b = new uchar 1 c 39 emit b 40 end 41 note emitb writes n in base b (2..36); a ulong has at most 64 digits in base 2 42 fn emitb n ulong b ulong to void 43 buf = new uchar 64 0 44 k = ulong 0 45 v = n 46 while true bound 64 47 put buf k get dig rem v b 48 k = add k 1 49 v = div v b 50 if eq v 0 51 break 52 end 53 end 54 for i ulong k 55 emitc get buf sub sub k 1 i 56 end 57 end 58 fn dsum n ulong b ulong to ulong 59 s = ulong 0 60 v = n 61 while gt v 0 bound 64 62 s = add s rem v b 63 v = div v b 64 end 65 ret s 66 end 67 fn tell n ulong b ulong to void 68 emitb n b 69 emit bs 70 emitu b 71 emit eqs 72 emitu dsum n b 73 emit nl 74 end 75 fn main to int 76 tell 1 10 77 tell 1234 10 78 tell 0xfe 16 79 tell 0xf0e 16 80 tell 255 8 81 tell `18446744073709551615` 2 82 tell `18446744073709551615` 36 83 ret 0 84 end 85 86 {{out}} 87

88 1 (base 10): digit sum 1

89 1234 (base 10): digit sum 10

90 fe (base 16): digit sum 29

91 f0e (base 16): digit sum 29

92 377 (base 8): digit sum 17

93 `1111111111111111111111111111111111111111111111111111111111111111` (base 2): digit sum 64

94 3w5e11264sgsf (base 36): digit sum 155

95