Anonymous user
Sum multiples of 3 and 5: Difference between revisions
m
→version 3: added/changed whitespace and comments, used templates for the output sections.
(Ada 202x version - Extra credit) |
m (→version 3: added/changed whitespace and comments, used templates for the output sections.) |
||
Line 3,316:
===version 3===
This version automatically adjusts the numeric digits.
The formula used is a form of the Gauss Summation formula.
<lang rexx>/*REXX program counts all integers from 1 ──► N─1 that are multiples of 3 or 5. */
parse arg N t . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N=
if t=='' | t=="," then t=
numeric digits 1000; w= 2 + length(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 m 'E' _ .
y= right( (m/1)'e' || (_+0), w)"-1"
z= n - 1; if t==1 then y=
say 'integers from 1 ──►' y " is " sumDiv(z,3) + sumDiv(z,5) - sumDiv(z,3*5)
N= N'0'
end /*t*/ /* [↑] simply append a zero to the num*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sumDiv: procedure; parse arg x,d; $= x % d; return d * $ * ($+1) % 2</lang>
<pre>
The sum of all positive integers that are a multiple of 3 and 5 are:
Line 3,341:
integers from 1 ──► 999 is 233168
</pre>
<pre style="height:80ex">▼
(Shown at three-quarter size.)
The sum of all positive integers that are a multiple of 3 and 5 are:
|