Sum multiples of 3 and 5: Difference between revisions

→‎{{header|UNIX Shell}}: Add implementation
(Emacs Lisp: Improve examples, replace redundant one with vanilla version)
(→‎{{header|UNIX Shell}}: Add implementation)
Line 4,274:
</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
{{works with|Zsh}}
Only works up to 1000000000 due to limits of shell integer representation.
 
<lang sh>function sum_multiples {
typeset -i n=$1 limit=$2
typeset -i max=limit-1
(( max -= max % n ))
printf '%d\n' $(( max / n * (n+max)/2 ))
}
 
function sum35 {
typeset -i limit=$1
printf '%d\n' $(( $(sum_multiples 3 $limit)
+ $(sum_multiples 5 $limit)
- $(sum_multiples 15 $limit) ))
}
 
for (( l=1; l<=1000000000; l*=10 )); do
printf '%10d\t%18d\n' "$l" "$(sum35 "$l")"
done</lang>
 
{{Out}}
<pre> 1 0
10 23
100 2318
1000 233168
10000 23331668
100000 2333316668
1000000 233333166668
10000000 23333331666668
100000000 2333333316666668
1000000000 233333333166666668</pre>
=={{header|VBA}}==
{{trans|VBScript}}
1,480

edits