Harshad or Niven series: Difference between revisions

Content added Content deleted
(→‎{{header|UNIX Shell}}: Add implementation)
Line 4,810: Line 4,810:
{{works with|Korn Shell}}
{{works with|Korn Shell}}
{{works with|Z Shell}}
{{works with|Z Shell}}
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">function main {
local -i i=0
function main {
gen_harshad | while read n; do
local -i i=0
if (( !i )); then
gen_harshad | while read n; do
if (( !i )); then
printf '%d' "$n"
printf '%d' "$n"
elif (( i < 20 )); then
elif (( i < 20 )); then
printf ' %d' "$n"
printf ' %d' "$n"
elif (( i == 20 )); then
elif (( i == 20 )); then
printf '\n'
elif (( n > 1000 )); then
printf '\n'
elif (( n > 1000 )); then
printf '%d\n' "$n"
printf '%d\n' "$n"
return
return
fi
fi
(( i++ ))
done
(( i++ ))
done
}
}


function is_harshad {
function is_harshad {
local -i sum=0 n=$1 i
local -i sum=0 n=$1 i
for (( i=0; i<${#n}; ++i )); do
for (( i=0; i<${#n}; ++i )); do
(( sum += ${n:$i:1} ))
(( sum += ${n:$i:1} ))
done
done
(( n % sum == 0 ))
(( n % sum == 0 ))
}
}


Line 4,851: Line 4,850:
1002
1002
</pre>
</pre>



=={{header|VBA}}==
=={{header|VBA}}==