Harshad or Niven series: Difference between revisions
Content added Content deleted
(→{{header|UNIX Shell}}: Add implementation) |
m (→{{header|UNIX Shell}}: Formatting) |
||
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 { |
||
⚫ | |||
function main { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
printf '%d' "$n" |
|||
elif (( i < 20 )); then |
|||
printf ' %d' "$n" |
|||
elif (( i == 20 )); then |
|||
printf '\n' |
|||
elif (( n > 1000 )); then |
|||
printf '\n' |
|||
printf '%d\n' "$n" |
|||
return |
|||
fi |
|||
(( i++ )) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
function is_harshad { |
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 )) |
|||
} |
} |
||
Line 4,851: | Line 4,850: | ||
1002 |
1002 |
||
</pre> |
</pre> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |