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. and  aA little extra code was added to format the output nicely.
 
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=1000 1000 /*Not specified? Then use the default.*/
if t=='' | t=="," then t= 1 1 /* " " " " " " */
numeric digits 1000; w= 2 + length(t) /*W: used for formatting 'e' part of Y.*/
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' _ . /*get the exponent.*/
y= right( (m/1)'e' || (_+0), w)"-1" /*this fixes a bug in a certain REXX. */
z= n - 1; if t==1 then y=z z /*handle a special case of a one─timer.*/
say 'integers from 1 ──►' y " is " sumDiv(z,3) + sumDiv(z,5) - sumDiv(z,3*5)
N= N'0' /*fast *10 multiply for next iteration.*/
end /*t*/ /* [↑] simply append a zero to the num*/
end /*t*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sumDiv: procedure; parse arg x,d; $= x % d; return d * $ * ($+1) % 2</lang>
'''{{out|output''' |text=&nbsp; when using the default input:}}
<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>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1 &nbsp; 85 </tt>}}
 
<pre style="height:80ex">
(Shown at three-quarter size.)
 
<pre style="font-size:75%;height:80ex188ex">
The sum of all positive integers that are a multiple of 3 and 5 are: