Sum multiples of 3 and 5: Difference between revisions
(+ D entry) |
m (Paddy3118 moved page Multiplies of 3 and 5 to Sum multiples of 3 and 5: Correct spelling and make clear.) |
(No difference)
|
Revision as of 22:16, 14 May 2013
The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below n. Show output for n = 1000.
BASIC
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:
467
D
<lang d>import std.stdio, std.range, std.algorithm;
long sum35(in long n) {
return n.iota.filter!q{ a % 3 == 0 || a % 5 == 0 }.reduce!q{a + b};
}
void main() {
1000.sum35.writeln;
}</lang>
- Output:
233168
Perl 6
<lang perl6>sub sum35($n) { [+] grep * %% (3|5), ^$n; }
say sum35 1000;</lang>
- Output:
233168
Python
Three ways of performing the calculation are shown including direct calculation of the value without having to do explicit sums in sum35c() <lang python>def sum35a(n):
'Direct count' # note: ranges go to n-1 return sum(x for x in range(n) if x%3==0 or x%5==0)
def sum35b(n):
"Count all the 3's; all the 5's; minus double-counted 3*5's" # note: ranges go to n-1 return sum(range(3, n, 3)) + sum(range(5, n, 5)) - sum(range(15, n, 15))
def sum35c(n):
'Sum the arithmetic progressions: sum3 + sum5 - sum15' consts = (3, 5, 15) # Note: stop at n-1 divs = [(n-1) // c for c in consts] sums = [d*c*(1+d)/2 for d,c in zip(divs, consts)] return sums[0] + sums[1] - sums[2]
- test
for n in range(1001):
sa, sb, sc = sum35a(n), sum35b(n), sum35c(n) assert sa == sb and sa == sc
print('For n = %i -> %i' % (n, sc))</lang>
- Output:
For n = 1000 -> 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