Sum multiples of 3 and 5: Difference between revisions

Content added Content deleted
(added Ol)
(→‎{{header|Python}}: Added an alternative version)
Line 2,604:
 
For n = 100000000000000000000 -> 2333333333333333333316666666666666666668</pre>
 
 
Or, more generally – taking the area under the straight line between the first multiple and the last:
 
<lang python># sum35 :: Int -> Int
def sum35(n):
f = sumMults(n)
return f(3) + f(5) - f(15)
 
 
# TEST ----------------------------------------------------
def main():
for x in (list(range(1, 6)) + list(range(18, 23))):
print(
'1e' + str(x) + '\t' + str(
sum35(10 ** x)
)
)
 
 
# sumMults :: Int -> Int -> Int
def sumMults(n):
"""Area under straight line between
first multiple and last"""
def go(n, m):
n1 = (n - 1) // m
return (m * n1 * (n1 + 1)) // 2
return lambda x: go(n, x)
 
 
if __name__ == '__main__':
main()</lang>
{{Out}}
<pre>1e1 23
1e2 2318
1e3 233168
1e4 23331668
1e5 2333316668
1e18 233333333333333333166666666666666668
1e19 23333333333333333331666666666666666668
1e20 2333333333333333333316666666666666666668
1e21 233333333333333333333166666666666666666668
1e22 23333333333333333333331666666666666666666668</pre>
 
=={{header|R}}==