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/harshad-or-niven-series/harshad-or-niven-series.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 / harshad-or-niven-series / harshad-or-niven-series.om` 66 lines ยท 1280 bytes Raw Blame History 1 note Rosetta Code task "Harshad or Niven series": https://rosettacode.org/wiki/Harshad_or_Niven_series 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text first "first 20 Harshad numbers:" 7 text above "first Harshad number above 1000: " 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 n is a Harshad number when its digit sum divides it 35 fn harshad n ulong to bool 36 s = ulong 0 37 v = n 38 while gt v 0 bound 20 39 s = add s rem v 10 40 v = div v 10 41 end 42 ret eq rem n s 0 43 end 44 note bound: every multiple of 10 up to 100 is Harshad, so neither search passes 1000 candidates 45 fn main to int 46 emit first 47 n = ulong 0 48 k = ulong 0 49 while lt k 20 bound 1000 50 n = add n 1 51 if harshad n 52 emit sp 53 emitu n 54 k = add k 1 55 end 56 end 57 emit nl 58 n = ulong 1001 59 while not harshad n bound 1000 60 n = add n 1 61 end 62 emit above 63 emitu n 64 emit nl 65 ret 0 66 end