Harshad or Niven series: Difference between revisions
add ABC
Not a robot (talk | contribs) (add ABC) |
|||
(25 intermediate revisions by 14 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,216 ⟶ 2,322:
1002
</pre>
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn Harshad( num as long ) as long
long sum = 0, tmp = num
while ( tmp > 0 )
sum += tmp mod 10
tmp = tmp / 10
wend
end fn = (num mod sum) = 0
local fn DoIt
long i = 1, cnt = 0
print "First 20 in series: ";
while (1)
if fn Harshad( i )
if ( cnt < 20 ) then print ; i; " "; : cnt++
if ( i > 1000 ) then print : print "First above 1000: "; i : exit while
end if
i++
wend
end fn
fn Doit
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
First 20 in series: 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|Fōrmulæ}}==
{{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 2,778 ⟶ 2,933:
1002
</pre>
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def harshad?
{def harshad?.sum
{lambda {:n}
{if {W.empty? {W.rest :n}}
then {W.first :n}
else {+ {W.first :n}
{harshad?.sum {W.rest :n}}} }}}
{lambda {:n}
{= {% :n {harshad?.sum :n}} 0} }}
-> harshad?
{harshad? 12}
-> true
{harshad? 13}
-> false
{def harshads
{def harshads.loop
{lambda {:a :n :i}
{if {> {A.length :a} :n}
then :a
else {harshads.loop {if {harshad? :i}
then {A.addlast! :i :a}
else :a}
:n
{+ :i 1}} }}}
{lambda {:n}
{harshads.loop {A.new} :n 0} }}
-> harshads
{harshads 20}
-> [0,1,2,3,4,5,6,7,8,9,10,12,18,20,21,24,27,30,36,40,42]
{def firstharshadafter
{def firstharshadafter.loop
{lambda {:i}
{if {harshad? :i}
then :i
else {firstharshadafter.loop {+ :i 1}} }}}
{lambda {:n}
{firstharshadafter.loop {+ :n 1}} }}
-> firstharshadafter
{firstharshadafter 1000}
-> 1002
</syntaxhighlight>
=={{header|LOLCODE}}==
Line 2,950 ⟶ 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 2,984 ⟶ 3,222:
digitSum = digitSum + val(mid$(y$,i,1))
next i
end function
function isHarshad(num)
isHarshad = num MOD digitSum(num)
end function
</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
=={{header|Modula-2}}==
Line 3,062 ⟶ 3,306:
</pre>
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout ("First 20: " ++ show first20 ++ "\n"),
Stdout ("First above 1000: " ++ show above1000 ++ "\n")]
first20 :: [num]
first20 = take 20 (filter isharshad [1..])
above1000 :: num
above1000 = hd (filter isharshad [1001..])
isharshad :: num->bool
isharshad n = n mod digitsum n = 0
digitsum :: num->num
digitsum 0 = 0
digitsum n = n mod 10 + digitsum (n div 10)</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 above 1000: 1002</pre>
=={{header|MLite}}==
<syntaxhighlight lang="ocaml">fun sumdigits
Line 3,187 ⟶ 3,452:
</syntaxhighlight>
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 ... 1002</pre>
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let is_harshad x =
let rec dsum n = if n < 10 then n else dsum (n / 10) + n mod 10 in
x mod dsum x = 0
let () =
let print_seq (x, n) =
Seq.(ints x |> filter is_harshad |> take n |> map string_of_int)
|> List.of_seq |> String.concat ", " |> print_endline
in
List.iter print_seq [1, 20; 1001, 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|Oforth}}==
Line 4,140 ⟶ 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,295 ⟶ 4,610:
42 is a Niven number
1002 is a Niven number
</pre>
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! Code
! Comments
|-
|
≪ '''DO'''
1 + DUP DUP →STR DUP SIZE → n len
≪ 0 1 len '''FOR''' j
n j DUP SUB NUM +
'''NEXT'''
len 48 * -
≫
'''UNTIL''' MOD NOT '''END'''
≫
'NXTHR' STO
≪ {} 0
1 20 '''START''' NXTHR SWAP OVER + SWAP '''NEXT''' DROP
1000 NXTHR
≫ EVAL
|
''( n -- next_Harshad_number)''
Increment n and initialize local variables
Add ASCII codes
Remove offset 48 = NUM("0")
Create list of first 20 Harshad numbers
Get first Harshad number > 1000
|}
{{out}}
<pre>
2: { 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 }
1: 1002
</pre>
Line 4,492 ⟶ 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
}
}
var iter = harshad()
say 20.of { iter.run }
var n
do {
n = iter.run
} while (n <= 1000)
say n
{{out}}
<pre>
Line 4,634 ⟶ 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,767 ⟶ 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 4,894 ⟶ 5,353:
=={{header|Wren}}==
<syntaxhighlight lang="
var n = 1
while (true) {
|