Sum multiples of 3 and 5: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|BASIC}}: direct conversation to discussion tab)
(→‎{{header|REXX}}: changed to below :-) thanks)
Line 37: Line 37:
mul35:
mul35:
s=0
s=0
Do i=1 To 1000
Do i=1 To 999
If i//3=0 | i//5=0 Then
If i//3=0 | i//5=0 Then
s=s+i
s=s+i
Line 43: Line 43:
Return s</lang>
Return s</lang>
Output:
Output:
<pre>234168</pre>
<pre>233168</pre>

Revision as of 16:34, 14 May 2013

Sum multiples of 3 and 5 is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The objective is to find the sum of multiplies of 3 or 5 below n within a function. Test it with n = 1000.

BASIC

Works with: FreeBASIC
This example is incorrect. Please fix the code and remove this message.

Details: (Or if the function is right, then the task description should say 'count of positive multiples of 3 or 5 no greater than a given number.' or some such...see discussion tab above.)


I'd suggest to change s += 1 to s += i
REXX also finds 467 numbers <lang basic>Declare function mulsum35(n as integer) as integer Function mulsum35(n as integer) as integer

   Dim s as integer
   For i as integer = 1 to n
       If (i mod 3 = 0) or (i mod 5 = 0) then
           s += 1
       End if
   Next i
   Return s

End Function Print mulsum35(1000) Sleep End</lang>

Output:
 46

Perl 6

<lang perl6>sub sum35($n) { [+] grep * %% (3|5), ^$n; }

say sum35 1000;</lang>

Output:
233168

REXX

<lang rexx>/* REXX ***************************************************************

  • 14.05.2013 Walter Pachl
                                                                                                                                            • /

Say mul35() exit mul35: s=0 Do i=1 To 999

 If i//3=0 | i//5=0 Then
   s=s+i
 End

Return s</lang> Output:

233168