Harshad or Niven series: Difference between revisions

m
syntax highlighting fixup automation
(Add VTL-2)
m (syntax highlighting fixup automation)
Line 32:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F is_harshad(n)
R n % sum(String(n).map(ch -> Int(ch))) == 0
 
Line 48:
I is_harshad(n)
print(n)
L.break</langsyntaxhighlight>
 
{{out}}
Line 57:
 
=={{header|360 Assembly}}==
<langsyntaxhighlight lang="360asm">* Harshad or Niven series - 01/05/2019
NIVEN CSECT
USING NIVEN,R13 base register
Line 113:
XDEC DS CL12 temp xdeco
REGEQU symbolic registers
END NIVEN</langsyntaxhighlight>
{{out}}
<pre>
Line 121:
 
=={{header|8080 Assembly}}==
<langsyntaxhighlight lang="asm"> cpu 8086
org 100h
section .text
Line 164:
section .data
db '*****'
buffer: db 13,10,'$'</langsyntaxhighlight>
{{out}}
<pre>1
Line 189:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">INT FUNC SumOfDigits(INT a)
INT sum
 
Line 223:
a=Next(1000)
PrintIE(a)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Harshad_or_Niven_series.png Screenshot from Atari 8-bit computer]
Line 231:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Harshad is
Line 256:
Current := 1000 + 1;
Ada.Text_IO.Put_Line(" ..." & Integer'Image(Next(Current)));
end Harshad;</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 ... 1002</pre>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN
PROC digit sum = (INT i) INT :
BEGIN
Line 273:
FOR i FROM 1001 DO
(i %* digit sum (i) = 0 | printf (($g(0)l$, i)); stop) OD
END</langsyntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 1002
Line 279:
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algolm">begin
integer function mod(a,b);
integer a,b;
Line 314:
 
write("First Harshad number above 1000:", nextharshad(1000));
end</langsyntaxhighlight>
{{out}}
<pre>First 20 Harshad numbers:
Line 322:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % find members of the Harshad/Niven series - numbers divisible by the sum of their digits %
% returns the next member of the series above n %
integer procedure nextHarshad ( integer value n ) ;
Line 349:
end for_i ;
write( i_w := 1, s_w := 0, "First Harshad/Niven number > 1000: ", nextHarshad( 1000 ) );
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 358:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">(20∘↑,¯1∘↑)∘((⊢,((+∘1)⍣((0=+/∘(⍎¨⍕)|⊢)⊣)⊃∘⌽))⍣(1∊1000<⊣)),1</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 1002</pre>
Line 364:
=={{header|AppleScript}}==
===Idiomatic===
<langsyntaxhighlight lang="applescript">on nextHarshad(n)
if (n < 0) then set n to 0
repeat
Line 388:
set h to nextHarshad(1000)
 
return {harshads, h}</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42}, 1002}</langsyntaxhighlight>
 
===Functional===
 
Clicking together some generic functions, for a little more productivity and for easier refactoring, we can define the Harshad series as a filter over an infinite stream of integers.
<langsyntaxhighlight lang="applescript">
------------------ HARSHAD OR NIVEN SERIES -----------------
 
Line 685:
set my text item delimiters to dlm
s
end unlines</langsyntaxhighlight>
{{Out}}
<pre>First 20: -> [1,2,3,4,5,6,7,8,9,10,12,18,20,21,24,27,30,36,40,42]
Line 693:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">harshad?: function [n] -> zero? n % sum digits n
harshads: select 1..1100 => harshad?
 
Line 703:
break
]
]</langsyntaxhighlight>
 
{{out}}
Line 711:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">H := []
n := 1
 
Line 731:
n++, sum := ""
}
}</langsyntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, . . . 1002</pre>
 
=={{header|AWK}}==
<langsyntaxhighlight AWKlang="awk">#!/usr/bin/awk -f
BEGIN {
k=0; n=0;
Line 757:
}
return !(n%s);
}</langsyntaxhighlight>
{{out}}
<pre>First twenty Harshad numbers are:
Line 765:
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">
@echo off
setlocal enabledelayedexpansion
Line 802:
set /a tempcount+=1
goto strlengthloop
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 829:
 
=={{header|BASIC}}==
<langsyntaxhighlight BASIClang="basic">10 DEFINT P,N,I,S
20 PRINT "First 20 Harshad numbers:"
30 P=0
Line 843:
130 IF N<=1000 THEN 50
140 PRINT
150 PRINT "First Harshad number > 1000:";N</langsyntaxhighlight>
{{out}}
<pre>First 20 Harshad numbers:
Line 856:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="freebasic">
function sumDigitos(n)
if n < 0 then return 0
Line 895:
until false
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 907:
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> I%=1:CNT%=0
WHILE TRUE
IF FNHarshad(I%) THEN
Line 925:
tmp%/=10
ENDWHILE
=(num% MOD sum%)=0</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 1002</pre>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let dsum(n) = n=0 -> 0, n rem 10 + dsum(n/10)
Line 943:
$)
writef("*NFirst above 1000: %N*N", next(1000))
$)</langsyntaxhighlight>
{{out}}
<pre>First 20: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 949:
 
=={{header|Befunge}}==
<langsyntaxhighlight lang="befunge">45*1>::01-\>:55+%\vv\0<
>\1+^ + <|:/<+55<` :
^_>1-\:.v@1>\:0\`#v_+\^
>^1\,+55<.^_:#%$:#<"}"v
^!:\_ ^###< !`*8<</langsyntaxhighlight>
 
{{out}}
Line 979:
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">SumDgt ← +´•Fmt-'0'˙
Niven ← 0=SumDgt|⊢
nivens ← Niven¨⊸/↕1100
⟨20↑nivens, ⊑(⊢>1000˙)⊸/nivens⟩</langsyntaxhighlight>
{{out}}
<pre>⟨ ⟨ 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 ⟩ 1002 ⟩</pre>
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
 
static int digsum(int n)
Line 1,008:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 1,014:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">
using System;
using System.Collections.Generic;
Line 1,076:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 1002</pre>
 
===Shorter solution===
<langsyntaxhighlight lang="csharp">using System.Collections.Generic;
using static System.Linq.Enumerable;
using static System.Console;
Line 1,102:
for (; n > 0; n /= 10) yield return n % 10;
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <vector>
#include <iostream>
 
Line 1,138:
std::cout << "The smallest Harshad number greater than 1000 : " << start << '\n' ;
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,147:
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn digsum [n acc]
(if (zero? n) acc
(digsum (quot n 10) (+ acc (mod n 10)))))
Line 1,155:
(iterate inc 1))]
(prn (take 20 harshads))
(prn (first (filter #(> % 1000) harshads))))</langsyntaxhighlight>
{{out}}
<pre>(1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42)
Line 1,161:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">digit_sum = proc (n: int) returns (int)
sum: int := 0
while n > 0 do
Line 1,193:
break
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>First 20 Harshad numbers:
Line 1,201:
=={{header|COBOL}}==
{{works with|OpenCOBOL|1.1}}
<langsyntaxhighlight lang="cobol">identification division.
program-id. harshad.
environment division.
Line 1,279:
*> end-if.
exit paragraph.
</langsyntaxhighlight>
 
=={{header|ColdFusion}}==
<langsyntaxhighlight lang="cfm">
<Cfset harshadNum = 0>
<Cfset counter = 0>
Line 1,322:
 
<Cfoutput>... #harshadNum# </Cfoutput>
</syntaxhighlight>
</lang>
 
=={{header|Comal}}==
<langsyntaxhighlight lang="comal">0010 FUNC digit'sum(n)
0020 sum:=0
0030 WHILE n>0 DO sum:+n MOD 10;n:=n DIV 10
Line 1,346:
0200 PRINT
0210 PRINT "First Harshad number above 1000:";next'harshad(1000)
0220 END</langsyntaxhighlight>
{{out}}
<pre>First 20 Harshad numbers:
Line 1,353:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun harshadp (n)
(zerop (rem n (digit-sum n))))
 
Line 1,364:
(cond ((= (length lst) n) (reverse lst))
((harshadp i) (list-harshad n (+ i 1) (cons i lst)))
(t (list-harshad n (+ i 1) lst))))</langsyntaxhighlight>{{out}}
<pre>CL-USER> (list-harshad 20)
(1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42)
Line 1,372:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub digitsum(n: uint16): (sum: uint8) is
Line 1,406:
print_nl();
print_i16(n);
print_nl();</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 1,413:
=={{header|Crystal}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">harshad = 1.step.select { |n| n % n.to_s.chars.sum(&.to_i) == 0 }
puts "The first 20 harshard numbers are: \n#{ harshad.first(20).to_a }"
puts "The first harshard number > 1000 is #{ harshad.find { |n| n > 1000 } }"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,426:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.algorithm, std.range, std.conv;
 
Line 1,434:
harshads.take(20).writeln;
harshads.filter!(h => h > 1000).front.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42]
Line 1,442:
 
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">proc nonrec dsum(word n) word:
word r;
r := 0;
Line 1,471:
writeln();
write("First above 1000: ", next_harshad(1000))
corp</langsyntaxhighlight>
{{out}}
<pre>First 20: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 1,477:
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(define (harsh? n)
(zero? (modulo n
Line 1,492:
(for ((n H)) #:break (> n 1000) => n)
→ 1002
</syntaxhighlight>
</lang>
 
=={{header|Eiffel}}==
<langsyntaxhighlight lang="eiffel">
note
description : "project application root class"
Line 1,573:
end
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,600:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Harshad do
def series, do: Stream.iterate(1, &(&1+1)) |> Stream.filter(&(number?(&1)))
Line 1,611:
IO.inspect Harshad.series |> Enum.take(20)
 
IO.inspect Harshad.series |> Stream.drop_while(&(&1 <= 1000)) |> Enum.take(1) |> hd</langsyntaxhighlight>
 
{{out}}
Line 1,620:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( harshad ).
 
Line 1,647:
sequence( _N, Found, {Found, Acc} ) -> lists:reverse( Acc );
sequence( N, Find, Acc ) -> sequence( N + 1, Find, acc(N, Acc) ).
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,660:
A somewhat more simple approach. Somewhat more efficient since it produces the partial list 23 times for the 20 element case whereas the above does so 36 or 37 times.
 
<syntaxhighlight lang="erlang">
<lang Erlang>
-module(harshad).
-export([main/0,harshad/1,seq/1]).
Line 1,691:
io:format("seq(20): ~w~n", [ seq(20) ]),
io:format("gt(1000): ~w~n", [ gt(1000,[]) ]).
</syntaxhighlight>
</lang>
<pre>
Line 1,709:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">nextHarshad
=LAMBDA(n,
UNTIL(
Line 1,735:
{1}
)
)</langsyntaxhighlight>
 
and also assuming the following generic bindings in the Name Manager for the WorkBook:
 
<langsyntaxhighlight lang="lisp">APPENDROWS
=LAMBDA(xs,
LAMBDA(ys,
Line 1,797:
)
)
)</langsyntaxhighlight>
 
{{Out}}
Line 1,919:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let divides d n =
match bigint.DivRem(n, d) with
| (_, rest) -> rest = 0I
Line 1,938:
printfn ""
printfn "%A" (Seq.find (fun n -> n > 1000I) harshads)
0</langsyntaxhighlight>
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
1002</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: math.text.utils lists lists.lazy ;
 
: niven? ( n -- ? ) dup 1 digit-groups sum mod 0 = ;
Line 1,953:
 
20 first-n-niven .
1000 next-niven .</langsyntaxhighlight>
{{out}}
<pre>
Line 1,962:
=={{header|FBSL}}==
The INITIALIZE routine fills a dynamic array with all we need, even the ellipsis.
<langsyntaxhighlight lang="qbasic">#APPTYPE CONSOLE
 
CLASS harshad
Line 2,009:
 
PAUSE
</syntaxhighlight>
</lang>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 ... 1002
Line 2,015:
 
=={{header|FOCAL}}==
<langsyntaxhighlight FOCALlang="focal">01.10 S N=0
01.20 S P=0
01.30 D 3
Line 2,034:
03.10 S N=N+1
03.20 D 2
03.30 I (FITR(N/A)*A-N)3.1</langsyntaxhighlight>
{{out}}
<pre>= 1
Line 2,063:
are in the comments at the START of the FORTRAN 2003 source.
The 1--20 loop idea was stolen from the ada solution. Thank you.
<syntaxhighlight lang="fortran">
<lang FORTRAN>
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Tue May 21 13:15:59
Line 2,108:
 
end program Harshad
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function sumDigits(n As Integer) As Integer
Line 2,154:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 2,166:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">
isHarshad[n] := n mod sum[integerDigits[n]] == 0
 
Line 2,189:
 
println[i]
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,227:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=9d814ce9936ed7fdce2a084004c437f4 Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim siCount, siLoop, siTotal, siCounter As Short
Dim sNo, sTemp As String
Line 2,254:
Print sNiven.Join(", ")
 
End</langsyntaxhighlight>
Output:
<pre>
Line 2,262:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 2,308:
}
fmt.Println(n)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,316:
 
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">
<lang Groovy>
​class HarshadNiven{ public static boolean find(int x)
{
Line 2,354:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,363:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Char (digitToInt)
 
harshads :: [Int]
Line 2,371:
 
main :: IO ()
main = mapM_ print [take 20 harshads, [(head . filter (> 1000)) harshads]]</langsyntaxhighlight>
{{out}}
<pre>[1,2,3,4,5,6,7,8,9,10,12,18,20,21,24,27,30,36,40,42]
Line 2,378:
Or, as an alternative to string operations:
 
<langsyntaxhighlight lang="haskell">import Data.List (unfoldr)
import Data.Tuple (swap)
import Data.Bool (bool)
Line 2,390:
 
main :: IO ()
main = mapM_ print $ [take 20, take 1 . dropWhile (<= 1000)] <*> [harshads]</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,398:
=={{header|Icon}} and {{header|Unicon}}==
 
<langsyntaxhighlight lang="unicon">procedure main(A)
limit := integer(A[1]) | 20
every writes(niven(seq())\limit," ")
Line 2,408:
n ? {s := 0; while s +:= move(1)}
if (n%s) = 0 then return n
end</langsyntaxhighlight>
 
{{out}}
Line 2,418:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "Harshad.bas"
110 LET I=1:LET CNT=0
120 PRINT "First 20 Harshad numbers are:"
Line 2,437:
270 LOOP
280 LET HARSHAD=MOD(NUM,SUM)=0
290 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j">Until =: 2 : 'u^:(-.@:v)^:_'
isHarshad =: 0 = ] |~ [: +/ #.inv NB. BASE isHarshad N
assert 1 0 -: 10 isHarshad&> 42 38
Line 2,455:
' '-.~":6#.inv nextHarshad_base_6 6b23235
23253
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">public class Harshad{
private static long sumDigits(long n){
long sum = 0;
Line 2,482:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1
Line 2,509:
=={{header|JavaScript}}==
===ES5===
<langsyntaxhighlight lang="javascript">function isHarshad(n) {
var s = 0;
var n_str = new String(n);
Line 2,533:
while (!isHarshad(++h));
console.log(h);
</syntaxhighlight>
</lang>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 2,539:
 
===ES6 with generator===
<langsyntaxhighlight JavaScriptlang="javascript">function* harshads (start) {
for (let n = start; true; n++) {
const sum = [...n.toString()].map(Number).reduce((a, b) => a + b)
Line 2,555:
 
const firstAfter1000 = harshads(1001).next().value
console.log("First after 1000:", firstAfter1000)</langsyntaxhighlight>
{{out}}
<pre>First 20: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 2,562:
===ES6===
One possible approach to functional composition:
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 2,630:
firstOver1000: head(dropWhile(x => x <= 1000, nHarshads(1000)))
});
})();</langsyntaxhighlight>
{{Out}}
<pre>{
Line 2,659:
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq">def is_harshad:
def digits: tostring | [explode[] | ([.]| implode) | tonumber];
if . >= 1 then (. % (digits|add)) == 0
Line 2,685:
 
# Task:
[ harshads(20), "...", harshad_greater_than(1000)]</langsyntaxhighlight>
{{Out}}
$ jq -n -c -f harshad.jq
Line 2,692:
=={{header|Julia}}==
{{works with|Julia|0.6}}
<langsyntaxhighlight lang="julia">isharshad(x) = x % sum(digits(x)) == 0
nextharshad(x) = begin while !isharshad(x+1) x += 1 end; return x + 1 end
 
Line 2,705:
 
println("First 20 harshad numbers: ", join(harshads(20), ", "))
println("First harshad number after 1001: ", nextharshad(1000))</langsyntaxhighlight>
 
{{out}}
Line 2,712:
 
=={{header|K}}==
<syntaxhighlight lang="k">
<lang K>
/ sum of digits of an integer
sumdig: {d::(); (0<){d::d,x!10; x%:10}/x; +/d}
Line 2,719:
/ Generate x Harshad numbers starting from y and display the list
hSeries: {harshad::();i:y;while[(x-#harshad)>0;:[isHarshad i; harshad::(harshad,i)]; i+:1];harshad}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,730:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1
 
fun sumDigits(n: Int): Int = when {
Line 2,768:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,780:
 
=={{header|LOLCODE}}==
<langsyntaxhighlight LOLCODElang="lolcode">HAI 1.3
 
HOW IZ I digsummin YR num
Line 2,814:
IM OUTTA YR finder
 
KTHXBYE</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 2,820:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function isHarshad(n)
local s=0
local n_str=tostring(n)
Line 2,848:
end
print(h)
</syntaxhighlight>
</lang>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 2,854:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">nextHarshad =
NestWhile[# + 1 &, # + 1, ! Divisible[#, Total@IntegerDigits@#] &] &;
Print@Rest@NestList[nextHarshad, 0, 20];
Print@nextHarshad@1000;</langsyntaxhighlight>
{{out}}
<pre>{1,2,3,4,5,6,7,8,9,10,12,18,20,21,24,27,30,36,40,42}
Line 2,863:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(A,B)
Line 2,904:
0 $34HFIRST HARSHAD NUMBER ABOVE 1000 = ,I4*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre>FIRST 20 HARSHAD NUMBERS:
Line 2,931:
=={{header|MATLAB}} / {{header|Octave}}==
Define a testing function whether n is harshad or not
<langsyntaxhighlight MATLABlang="matlab">function v = isharshad(n)
v = isinteger(n) && ~mod(n,sum(num2str(n)-'0'));
end; </langsyntaxhighlight>
Check numbers
<langsyntaxhighlight MATLABlang="matlab">k=1; n=1;
while (k<=20)
if isharshad(n)
Line 2,947:
n=n+1;
end;
printf('\nFirst harshad number larger than 1000 is %i\n',n);</langsyntaxhighlight>
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
First harshad number larger than 1000 is 1002</pre>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Harshad;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
 
Line 2,987:
WriteCard(NextHarshad(1000), 4);
WriteLn();
END Harshad.</langsyntaxhighlight>
{{out}}
<pre>First 20 Harshad numbers:
Line 2,995:
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">(
:n () =list
(n 0 >) (
Line 3,015:
 
0 (next-harshad print " " print!) 20 times newline
1000 next-harshad print!</langsyntaxhighlight>
{{out}}
<pre>
Line 3,023:
 
=={{header|MLite}}==
<langsyntaxhighlight lang="ocaml">fun sumdigits
(0, n) = n
| (m, n) = sumdigits (m div 10, m rem 10) + n
Line 3,049:
 
print "first twenty harshad numbers = "; println ` harshad 20;
print "first harshad number after 1000 = "; println ` next_harshad_after 1000;</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight lang="netrexx">/* NetRexx ------------------------------------------------------------
* 21.01.2014 Walter Pachl translated from ooRexx (from REXX version 1)
*--------------------------------------------------------------------*/
Line 3,086:
sum=sum+n.substr(k,1)
End
Return sum</langsyntaxhighlight>
'''output''' same as ooRexx's
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">proc slice[T](iter: iterator(): T {.closure.}; sl: Slice[T]): seq[T] =
var i = 0
for n in iter():
Line 3,110:
if n > 1000:
echo n
break</langsyntaxhighlight>
{{out}}
<pre>@[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42]
Line 3,116:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
class Harshad {
function : Main(args : String[]) ~ Nil {
Line 3,145:
}
}
</syntaxhighlight>
</lang>
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 ... 1002</pre>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: sumDigits(n) 0 while(n) [ n 10 /mod ->n + ] ;
: isHarshad dup sumDigits mod 0 == ;
 
1100 seq filter(#isHarshad) dup left(20) println dup filter(#[ 1000 > ]) first println</langsyntaxhighlight>
 
{{out}}
Line 3,162:
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">/* REXX ---------------------------------------------------------------
* 21.01.2014 Walter Pachl modi-(simpli-)fied from REXX version 1
*--------------------------------------------------------------------*/
Line 3,194:
sum=sum+substr(n,k,1)
End
Return sum</langsyntaxhighlight>
{{out}}
<pre>first 20 Niven numbers: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 3,201:
=={{header|PARI/GP}}==
{{works with|PARI/GP|2.6.0 and above}}
<langsyntaxhighlight lang="parigp">isHarshad(n)=n%sumdigits(n)==0
n=0;k=20;while(k,if(isHarshad(n++),k--;print1(n", ")));
n=1000;while(!isHarshad(n++),);print("\n"n)</langsyntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42,
Line 3,213:
 
Optimized for speed, by using the state before in IncSumDigit.
<langsyntaxhighlight lang="pascal">program Niven;
 
{$IFDEF FPC}
Line 3,359:
~ 24 Cpu-cycles per test i3- 4330 1..2^32-1}
{$IFNDEF LINUX}readln;{$ENDIF}
end.</langsyntaxhighlight>
output:
<pre>1.2.3.4.5.6.7.8.9.10.12.18.20.21.24.27.30.36.40.42.....1002.</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use strict ;
use warnings ;
Line 3,384:
print "$harshadnumbers[ $i ]\n" ;
}
print "The first Harshad number greater than 1000 is $harshadnumbers[ -1 ]!\n" ;</langsyntaxhighlight>
{{out}}
<pre>1
Line 3,409:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">={</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span>
Line 3,441:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">n</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 3,448:
</pre>
Alternative version
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">function</span> <span style="color: #000000;">isHarshad</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)))=</span><span style="color: #000000;">0</span>
Line 3,464:
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">isHarshad</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span><span style="color: #0000FF;">&</span><span style="color: #000000;">n</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 3,471:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">
<lang PicoLisp>
#if niven number, return it.
(de niven (N)
Line 3,487:
(printsp ~(list ~(head 20
(nivGen 1 1000) ) (max ~(nivGen 1001 1010)) ) )
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,494:
 
=={{header|PILOT}}==
<langsyntaxhighlight lang="pilot">C :n=0
:i=0
*first20
Line 3,515:
J (r):*digit
J (n<>s*(n/s)):*harshad
E :</langsyntaxhighlight>
{{out}}
<pre>1: 1
Line 3,540:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">*process source or(!) xref attributes;
niven: Proc Options(main);
/*********************************************************************
Line 3,591:
End;
 
End;</langsyntaxhighlight>
 
{{out}}
Line 3,598:
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plm">100H:
 
/* FIND THE SUM OF THE DIGITS OF A 16-BIT NUMBER */
Line 3,663:
 
CALL BDOS(0,0);
EOF</langsyntaxhighlight>
{{out}}
<pre>FIRST 20: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 3,671:
{{works with|PowerShell|2}}
In PowerShell, we generally don't wrap every little thing in a function. If you have something simple to do, you just do it.
<syntaxhighlight lang="powershell">
<lang PowerShell>
1..1000 | Where { $_ % ( [int[]][string[]][char[]][string]$_ | Measure -Sum ).Sum -eq 0 } | Select -First 20
1001..2000 | Where { $_ % ( [int[]][string[]][char[]][string]$_ | Measure -Sum ).Sum -eq 0 } | Select -First 1
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,700:
</pre>
But if we do have a need for the code to be reusable, we can do that.
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Get-HarshadNumbers
{
Line 3,742:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
Get-HarshadNumbers -Count 20
Get-HarshadNumbers -Minimum 1001 -Count 1
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,774:
=={{header|Prolog}}==
Works with SWI-Prolog and module lambda.pl written by '''Ulrich Neumerkel''', it can be found there : http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl.
<langsyntaxhighlight Prologlang="prolog">:- use_module(library(lambda)).
 
niven :-
Line 3,816:
sum_list(LN, S).
 
</syntaxhighlight>
</lang>
{{out}}
<pre> ?- niven.
Line 3,844:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">If OpenConsole()=0 : End 1 : EndIf
 
Procedure.i Niven(v.i)
Line 3,859:
ForEver
 
Input()</langsyntaxhighlight>
{{out}}<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 ... 1002 </pre>
 
=={{header|Python}}==
===Python: Procedural===
<langsyntaxhighlight lang="python">>>> import itertools
>>> def harshad():
for n in itertools.count(1):
Line 3,880:
1002
>>> </langsyntaxhighlight>
 
===Python: Functional===
The for loop above [http://paddy3118.blogspot.co.uk/2013/03/itertoolsfirst.html could be changed] to the following to find the number > 1000; in fact the harshad generator function could become a generator expression creating this more functional version:
<langsyntaxhighlight lang="python">>>> from itertools import count, islice
>>> harshad = (n for n in count(1) if n % sum(int(ch) for ch in str(n)) == 0)
>>> list(islice(harshad, 0, 20))
Line 3,890:
>>> next(x for x in harshad if x > 1000)
1002
>>> </langsyntaxhighlight>
 
And we could also sum digits more directly (without string coercion) while still preserving functional composition:
 
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Harshad or Niven series'''
 
from itertools import count, dropwhile, islice
Line 4,007:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Harshad or Niven series:
Line 4,015:
 
=={{header|Quackery}}==
<langsyntaxhighlight Quackerylang="quackery">[ number$ $ "0 " swap
witheach
[ join $ " + " join ]
Line 4,039:
again ]
cr
</syntaxhighlight>
</lang>
'''Output:'''
<pre>The first 20 Harshad numbers are: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 4,049:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="scheme">#lang racket
 
(define (digsum n)
Line 4,061:
; First harshad greater than 1000
(displayln (for/first ([h harshads] #:when(> h 1000)) h))</langsyntaxhighlight>
 
{{out}}
Line 4,069:
Different to the Scheme implementation in that it illustrates Racket's native iterators,
and ''let-values'' with ''quotient/remainder'':
<langsyntaxhighlight lang="racket">#lang racket
(require math/number-theory)
(define (digital-sum n)
Line 4,088:
 
;; find 1st Harshad number > 1000
(displayln (for/first ((h (sequence-filter harshad-number? (in-naturals 1001)))) h))</langsyntaxhighlight>
{{out}}
<pre>#1 1
Line 4,116:
 
{{works with|Rakudo|2016.08}}
<syntaxhighlight lang="raku" perl6line>constant @harshad = grep { $_ %% .comb.sum }, 1 .. *;
say @harshad[^20];
say @harshad.first: * > 1000;</langsyntaxhighlight>
{{out}}
<pre>(1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42)
Line 4,130:
Also, gihugeic integers are supported &nbsp; (essentially no limit).
===generic===
<langsyntaxhighlight lang="rexx">/*REXX program finds the first A Niven numbers; it also finds first Niven number > B.*/
parse arg A B . /*obtain optional arguments from the CL*/
if A=='' | A==',' then A= 20 /*Not specified? Then use the default.*/
Line 4,145:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sumDigs: parse arg x 1 s 2 q; do k=1 for length(q); s= s+substr(q,k,1); end; return s</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 4,154:
===idiomatic===
This REXX version idiomatically uses a &nbsp; '''isNiven''' &nbsp; function.
<langsyntaxhighlight lang="rexx">/*REXX program finds the first A Niven numbers; it also finds first Niven number > B.*/
parse arg A B . /*obtain optional arguments from the CL*/
if A=='' | A==',' then A= 20 /*Not specified? Then use the default.*/
Line 4,170:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isNiven: parse arg x 1 s 2 q; do k=1 for length(q); s=s+substr(q,k,1); end; return x//s==0</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
Line 4,176:
This REXX version optimizes the &nbsp; '''isNiven''' &nbsp; function by using &nbsp; '''parse''' &nbsp; statements instead of the &nbsp; '''substr''' &nbsp; BIF,
<br>yielding a faster algorithm.
<langsyntaxhighlight lang="rexx">/*REXX program finds the first A Niven numbers; it also finds first Niven number > B.*/
parse arg A B . /*obtain optional arguments from the CL*/
if A=='' | A==',' then A= 20 /*Not specified? Then use the default.*/
Line 4,195:
do length(q); parse var q _ 2 q; sum= sum + _
end /*length(q)*/ /* ↑ */
return x // sum == 0 /* └──────◄ is destructively parsed. */</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
Line 4,202:
 
In addition, if the &nbsp; '''A''' &nbsp; number is negative, the numbers in the array aren't displayed, but the &nbsp; ''last'' &nbsp; number in the array is displayed.
<langsyntaxhighlight lang="rexx">/*REXX program finds the first A Niven numbers; it also finds first Niven number > B.*/
parse arg A B . /*obtain optional arguments from the CL*/
if A=='' | A==',' then A= 20 /*Not specified? Then use the default.*/
Line 4,227:
do while q\==''; parse var q _ 2 q; sum= sum + _
end /*while*/ /* ↑ */
return x // sum == 0 /* └──────◄ is destructively parsed.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when the input used is: &nbsp; &nbsp; <tt> -1000000 &nbsp; 66777888 </tt>}}
<pre>
Line 4,236:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
i = 1
count = 0
Line 4,254:
niv = ((nr % sum) = 0)
return niv
</syntaxhighlight>
</lang>
Output:
<pre>
Line 4,283:
{{works with|Ruby|2.4}}
Ruby 2.4 gave Integers a '''digits''' method, and Arrays a '''sum''' method.
<langsyntaxhighlight Rubylang="ruby">harshad = 1.step.lazy.select { |n| n % n.digits.sum == 0 }
 
puts "The first 20 harshard numbers are: \n#{ harshad.first(20) }"
puts "The first harshard number > 1000 is #{ harshad.find { |n| n > 1000 } }"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,296:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">while count < 20
h = h + 1
if neven(h) = 0 then
Line 4,319:
next i
neven = h mod d
end function</langsyntaxhighlight>
{{out}}
<pre>
Line 4,346:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
<lang Rust>
fn is_harshad (n : u32) -> bool {
let sum_digits = n.to_string()
Line 4,363:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,390:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object Harshad extends App {
val harshads = Stream.from(1).filter(i => i % i.toString.map(_.asDigit).sum == 0)
 
println(harshads.take(20).toList)
println(harshads.filter(_ > 1000).head)
}</langsyntaxhighlight>
{{out}}
<pre>List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42)
Line 4,402:
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">#!/usr/local/bin/gosh
 
;; Show the first 20 niven numbers and the
Line 4,427:
(apply + (map string->number (map string (string->list (number->string n))))))
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,436:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func integer: sumOfDigits (in var integer: num) is func
Line 4,469:
current := 1001;
writeln(" ... " <& nextHarshadNum(current));
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,477:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func harshad() {
var n = 0;
{
Line 4,493:
} while (n <= 1000);
 
say n;</langsyntaxhighlight>
{{out}}
<pre>
Line 4,502:
=={{header|Sinclair ZX81 BASIC}}==
Works with 1k of RAM. <code>FAST</code> isn't all that fast.
<langsyntaxhighlight lang="basic"> 10 FAST
20 LET N=0
30 LET H=0
Line 4,516:
130 IF N>1000 THEN GOTO 150
140 GOTO 40
150 SLOW</langsyntaxhighlight>
{{out}}
<pre>1
Line 4,542:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">struct Harshad: Sequence, IteratorProtocol {
private var i = 0
 
Line 4,557:
 
print("First 20: \(Array(Harshad().prefix(20)))")
print("First over a 1000: \(Harshad().first(where: { $0 > 1000 })!)")</langsyntaxhighlight>
 
{{out}}
Line 4,565:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl"># Determine if the given number is a member of the class of Harshad numbers
proc isHarshad {n} {
if {$n < 1} {return false}
Line 4,582:
# Get the first value greater than 1000 that satisfies the condition
for {set n 1000} {![isHarshad [incr n]]} {} {}
puts "First Harshad > 1000 = $n"</langsyntaxhighlight>
{{out}}
<pre>
Line 4,590:
 
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">C=0
 
For I = 1 Step 1 Until C = 20 ' First 20 Harshad numbers
Line 4,612:
Loop
 
Return ((a@ % b@) = 0)</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 1002
Line 4,619:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Option Explicit
 
Sub Main()
Line 4,643:
Next i
IsHarshad = sNumber Mod Summ = 0
End Function</langsyntaxhighlight>
 
{{out}}
Line 4,651:
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">n = 0
m = 1
first20 = ""
Line 4,686:
IsHarshad = True
End If
End Function</langsyntaxhighlight>
 
{{out}}
Line 4,695:
 
=={{header|Visual FoxPro}}==
<langsyntaxhighlight lang="vfp">
LOCAL lnCount As Integer, k As Integer
CLEAR
Line 4,725:
RETURN n % d = 0
ENDFUNC
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,753:
 
=={{header|VTL-2}}==
<langsyntaxhighlight VTL2lang="vtl2">10 ?="First 20: ";
20 N=0
30 I=0
Line 4,775:
260 #=0<K*240
270 #=N/S*0+0<%*210
280 #=;</langsyntaxhighlight>
{{out}}
<pre>First 20: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 4,781:
 
=={{header|Whitespace}}==
<syntaxhighlight lang="whitespace">
<lang Whitespace>
Line 4,843:
 
</syntaxhighlight>
</lang>
This solution was generated from the pseudo-Assembly below.
A [http://ideone.com/AKxEMY live run] is available for the inquiring skeptic.
<langsyntaxhighlight lang="asm">push 0 ; Harshad numbers found
push 0 ; counter
 
Line 4,871:
 
3: ; Print the > 1000 Harshad number on its own line and exit clean.
push 10 ochr onum pop push 10 ochr exit</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
Line 4,877:
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">var niven = Fiber.new {
var n = 1
while (true) {
Line 4,902:
break
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,913:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
int H, C, N, S; \Harshad number, Counter, Number, Sum
[H:= 1; C:= 0;
Line 4,926:
H:= H+1;
];
]</langsyntaxhighlight>
 
{{out}}
Line 4,936:
=={{header|Yabasic}}==
{{trans|BASIC256}}
<syntaxhighlight lang="yabasic">
<lang Yabasic>
sub sumDigits(n)
if n < 0 then return 0 : endif
Line 4,976:
print
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,984:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn harshad(n){ 0==n%(n.split().sum(0)) }
[1..].tweak(fcn(n){ if(not harshad(n)) return(Void.Skip); n })
.walk(20).println();
[1..].filter(20,harshad).println();
[1001..].filter1(harshad).println();</langsyntaxhighlight>
Walkers are zkl iterators. [a..b] is a Walker from a to b. Walkers can be tweaked to transform the sequence they are walking. In this case, ignore non Harshad numbers. Then tell the walker to get 20 items from that [modified] sequence.
 
Line 5,001:
=={{header|ZX Spectrum Basic}}==
{{trans|AWK}}
<langsyntaxhighlight lang="zxbasic">10 LET k=0: LET n=0
20 IF k=20 THEN GO TO 60
30 LET n=n+1: GO SUB 1000
Line 5,017:
1050 LET isHarshad=NOT FN m(n,s)
1060 RETURN
1100 DEF FN m(a,b)=a-INT (a/b)*b</langsyntaxhighlight>
10,327

edits