Anonymous user
Sum multiples of 3 and 5: Difference between revisions
m
→version 3: added/changed whitespace and comments.
(Added Emacs Lisp) |
m (→version 3: added/changed whitespace and comments.) |
||
Line 1,856:
<br>A little extra code was added to format the output nicely.
<br>The formula used is a form of the Gauss Summation formula.
<lang rexx>/*REXX pgm sums all integers from
parse arg N t .; if N=='' then N=1000; if t=='' then t=1
numeric digits 9999; numeric digits max(9,20*length(N*10**t))
say 'The sum of all positive integers that are a multiple of 3 and 5 are:'
say /* [↓] change the format/look of nE+nn
do t; parse value format(N,2,1,,0) 'E0' with
sum=sumDivisors(N-1, 3) + sumDivisors(N-1, 5) - sumDivisors(N-1, 3*5)
say 'integers from 1 ──►' y " is " sum
N=N*10 /*multiply by ten for the next round. */
end /*t*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
sumDivisors: procedure; parse arg x,d; _=x%d; return d*_*(_+1)%2</lang>
'''output''' when using the default input:
<pre>
The sum of all positive integers that are a multiple of 3 and 5 are:
Line 1,877:
integers from 1 ──► 999 is 233168
</pre>
'''output''' when using the input of: <tt> 1 80 </tt>
<pre style="height:80ex">
The sum of all positive integers that are a multiple of 3 and 5 are:
|