Sum multiples of 3 and 5: Difference between revisions

Content added Content deleted
(Ada 202x version - Extra credit)
m (→‎version 3: added/changed whitespace and comments, used templates for the output sections.)
Line 3,316: Line 3,316:


===version 3===
===version 3===
This version automatically adjusts the numeric digits. and a little extra code was added to format the output nicely.
This version automatically adjusts the numeric digits.   A little extra code was added to format the output nicely.


The formula used is a form of the Gauss Summation formula.
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. */
<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*/
parse arg N t . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N=1000 /*Not specified? Then use the default.*/
if N=='' | N=="," then N= 1000 /*Not specified? Then use the default.*/
if t=='' | t=="," then t= 1 /* " " " " " " */
if t=='' | t=="," then t= 1 /* " " " " " " */
numeric digits 1000; w=2+length(t) /*W: used for formatting 'e' part of Y.*/
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 'The sum of all positive integers that are a multiple of 3 and 5 are:'
say /* [↓] change the format/look of nE+nn*/
say /* [↓] change the format/look of nE+nn*/
do t; parse value format(N,2,1,,0) 'E0' with m 'E' _ . /*get the exponent.*/
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. */
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 /*handle a special case of a one─timer.*/
z= n - 1; if t==1 then y= 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)
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.*/
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. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
sumDiv: procedure; parse arg x,d; $=x % d; return d * $ * ($+1) % 2</lang>
sumDiv: procedure; parse arg x,d; $= x % d; return d * $ * ($+1) % 2</lang>
'''output''' &nbsp; when using the default input:
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
The sum of all positive integers that are a multiple of 3 and 5 are:
The sum of all positive integers that are a multiple of 3 and 5 are:
Line 3,341: Line 3,341:
integers from 1 ──► 999 is 233168
integers from 1 ──► 999 is 233168
</pre>
</pre>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 1 &nbsp; 85 </tt>
{{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:188ex">
The sum of all positive integers that are a multiple of 3 and 5 are:
The sum of all positive integers that are a multiple of 3 and 5 are: