Harshad or Niven series: Difference between revisions

add ABC
(Add Miranda)
(add ABC)
 
(16 intermediate revisions by 9 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,256 ⟶ 2,362:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Harshad_numbers}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Harshad numbers 01.png]]
In '''[https://formulae.org/?example=Harshad_numbers this]''' page you can see the program(s) related to this task and their results.
 
'''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,036 ⟶ 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,270 ⟶ 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,667 ⟶ 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 4,809 ⟶ 5,012:
 
0 OK, 0:185</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
{{works with|Z Shell}}
<syntaxhighlight lang="bash">function main {
local -i i=0 n
gen_harshad | while read n; do
if (( !i )); then
printf '%d' "$n"
elif (( i < 20 )); then
printf ' %d' "$n"
elif (( i == 20 )); then
printf '\n'
elif (( n > 1000 )); then
printf '%d\n' "$n"
return
fi
(( i++ ))
done
}
 
function is_harshad {
local -i sum=0 n=$1 i
for (( i=0; i<${#n}; ++i )); do
(( sum += ${n:$i:1} ))
done
(( n % sum == 0 ))
}
 
function gen_harshad {
local -i i=1
while true; do
if is_harshad $i; then
printf '%d\n' "$i"
fi
(( i++ ))
done
}
 
main "$@"</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|VBA}}==
Line 4,942 ⟶ 5,190:
20 42
First such number > 1000: 1002
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Zig">
fn main() {
mut count, mut i := 0, 0
print("The first 20 Harshad numbers: ")
for {
i++
if is_harshad(i) == true {
if count < 20 {print("${i} ") count++}
if i > 1000 {print("\nThe first Harshad number above 1000: ${i}") break}
}
}
}
 
fn sum_digits(number int) int {
mut num, mut sum := number, 0
if number <= 0 {return 0}
for num > 0 {
sum += num % 10
num /= 10
}
return sum
}
 
fn is_harshad(n int) bool {
if n % sum_digits(n) == 0 {return true}
return false
}
</syntaxhighlight>
 
{{out}}
<pre>
The first 20 Harshad numbers: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
The first Harshad number above 1000: 1002
</pre>
 
Line 5,069 ⟶ 5,353:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var niven = Fiber.new {
var n = 1
while (true) {
2,096

edits