Harshad or Niven series: Difference between revisions

add ABC
(add ABC)
 
(11 intermediate revisions by 7 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,410 ⟶ 1,436:
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
1002</pre>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">for i = 1 to 1002
 
let t = i
let s = 0
 
do
 
let s = s + t % 10
let t = int(t / 10)
 
wait
 
loop t > 0
 
if i % s = 0 and (c < 20 or i > 1000) then
 
let c = c + 1
print c, " : ", i
 
endif
 
next i</syntaxhighlight>
{{out| Output}}<pre>1 : 1
2 : 2
3 : 3
4 : 4
5 : 5
6 : 6
7 : 7
8 : 8
9 : 9
10 : 10
11 : 12
12 : 18
13 : 20
14 : 21
15 : 24
16 : 27
17 : 30
18 : 36
19 : 40
20 : 42
21 : 1002</pre>
 
=={{header|Crystal}}==
Line 1,475 ⟶ 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 2,257 ⟶ 2,363:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Harshad_numbers}}
 
'''Solution'''
 
[[File:Fōrmulæ - Harshad numbers 01.png]]
 
'''Test case 1.''' List the first 20 members of the sequence
 
[[File:Fōrmulæ - Harshad numbers 02.png]]
 
[[File:Fōrmulæ - Harshad numbers 03.png]]
 
'''Test case 2.''' List the first Harshad number greater than 1,000
 
[[File:Fōrmulæ - Harshad numbers 04.png]]
 
[[File:Fōrmulæ - Harshad numbers 05.png]]
 
=={{header|Gambas}}==
Line 3,032 ⟶ 3,154:
<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|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that returns a list of digits given a nonnegative integer */
decompose(num) := block([digits, remainder],
digits: [],
while num > 0 do
(remainder: mod(num, 10),
digits: cons(remainder, digits),
num: floor(num/10)),
digits
)$
 
/* Function that returns a list of the first len Harshad numbers */
harshad_count(len):=block(
[i:1,count:0,result:[]],
while count<len do (if map(lambda([x],if mod(x,apply("+",decompose(x)))=0 then true),[i])=[true] then (result:endcons(i,result),count:count+1),i:i+1),
result)$
 
/* Function that returns a list of the Harshad numbers up to len */
first_count(len):=block(
[i:1,count:0,result:[]],
while i<=len do (if map(lambda([x],if mod(x,apply("+",decompose(x)))=0 then true),[i])=[true] then (result:endcons(i,result),count:count+1),i:i+1),
length(result))$
 
/* Test cases */
harshad_count(20);
block(first_count(1000),last(harshad_count(%%+1)));
</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|MMBasic}}==
Line 4,266 ⟶ 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,663 ⟶ 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,146 ⟶ 5,353:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var niven = Fiber.new {
var n = 1
while (true) {
2,114

edits