Harshad or Niven series: Difference between revisions

add ABC
imported>Maxima enthusiast
No edit summary
(add ABC)
 
(7 intermediate revisions by 4 users not shown)
Line 188:
1002</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN digit.sum n:
PUT 0 IN sum
WHILE n>0:
PUT sum + (n mod 10) IN sum
PUT floor (n/10) IN n
RETURN sum
 
HOW TO REPORT harshad n:
REPORT n mod digit.sum n = 0
 
HOW TO RETURN next.harshad n:
PUT n+1 IN n
WHILE NOT harshad n: PUT n+1 IN n
RETURN n
 
PUT 0 IN n
WRITE "First 20 Harshad numbers:"
FOR i IN {1..20}:
PUT next.harshad n IN n
WRITE n
WRITE /
WRITE "First Harshad number > 1000:", next.harshad 1000/</syntaxhighlight>
{{out}}
<pre>First 20 Harshad numbers: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
First Harshad number > 1000: 1002</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">INT FUNC SumOfDigits(INT a)
Line 1,520 ⟶ 1,546:
<pre>First 20: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
First above 1000: 1002</pre>
 
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight>
func digsum n .
while n > 0
sum += n mod 10
n = n div 10
.
return sum
.
func isHarshad n .
return if n mod digsum n = 0
.
i = 1
repeat
if isHarshad i = 1
write i & " "
cnt += 1
.
until cnt = 20
i += 1
.
print ""
i = 1001
while isHarshad i = 0
i += 1
.
print i
</syntaxhighlight>
{{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|EchoLisp}}==
Line 4,361 ⟶ 4,422:
<pre>(1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42)
1002</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout 'First 20: ' <GetFirst 20 Harshad>>
<Prout 'First > 1000: ' <Next Harshad 1000>>;
};
 
GetFirst {
s.N s.F = <GetFirst s.N s.F 0>;
0 s.F s.Cur = ;
s.N s.F s.Cur, <Next s.F s.Cur>: s.Next
= s.Next <GetFirst <- s.N 1> s.F s.Next>;
};
 
Next {
s.F s.N, <+ 1 s.N>: s.Next, <Mu s.F s.Next>: {
T = s.Next;
F = <Next s.F s.Next>;
};
};
 
Harshad {
s.N, <DigSum s.N>: s.Dsum, <Mod s.N s.Dsum>: 0 = T;
s.N = F;
};
 
DigSum {
0 = 0;
s.N, <Divmod s.N 10>: (s.Rest) s.Dgt = <+ s.Dgt <DigSum s.Rest>>;
};</syntaxhighlight>
{{out}}
<pre>First 20: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
First > 1000: 1002</pre>
 
=={{header|REXX}}==
Line 4,758 ⟶ 4,852:
1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 ... 1002
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program harshad;
print("First 20 Harshad numbers:", [n := next(n) : i in [1..20]]);
print("First Harshad number >1000:", next(1000));
 
proc next(n);
(until harshad(n)) n +:= 1; end;
return n;
end proc;
 
proc harshad(n);
return n mod +/[val d : d in str n] = 0;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>First 20 Harshad numbers: [1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42]
First Harshad number >1000: 1002</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func harshad() {
var n = 0;
{
++n while !(n %% n.digits.sum.divides(n);
n;
}
}
 
var iter = harshad();
say 20.of { iter.run };
 
var n;
do {
n = iter.run
} while (n <= 1000);
 
say n;</syntaxhighlight>
{{out}}
<pre>
Line 5,241 ⟶ 5,353:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var niven = Fiber.new {
var n = 1
while (true) {
2,094

edits