Sum multiples of 3 and 5: Difference between revisions

→‎{{header|Python}}: Pylinted for Python 3, updated output
(Added Q solution.)
(→‎{{header|Python}}: Pylinted for Python 3, updated output)
Line 2,607:
 
Or, more generally – taking the area under the straight line between the first multiple and the last:
{{Works with|Python|3.7}}
<lang python>'''Summed multiples of 3 and 5 up to n'''
 
 
<lang python># sum35 :: Int -> Int
def sum35(n):
'''Sum of all positive multiples
of 3 or 5 below )n.
)'''
f = sumMults(n)
return f(3) + f(5) - f(15)
 
 
# TEST ----------------------------------------------------
def main():
for x in enumFromTo(1)(5) + enumFromTo(18)(25):
print(
'1e' + str(x) + '\t' + str(
sum35(10 ** x)
)
)
 
 
# sumMults :: Int -> Int -> Int
def sumMults(n):
"""'''Area under a straight line between
the first multiple and the last""".
'''
def go(n, m):
n1 = (n - 1) // m
return (m * n1 * (n1 + 1)) // 2
return lambda x: go(n, x)
 
 
# TEST ----------------------------------------------------
def main():
'''Tests for [10^1 .. 10^5], and [10^8 .. 10^25]
'''
print(
fTable(__doc__ + ':\n')(lambda x: '10E' + str(x))(
str
)(compose(sum35)(lambda x: 10**x))(
for x in enumFromTo(1)(5) + enumFromTo(18)(25):
)
)
 
 
# GENERIC -------------------------------------------------
 
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
def compose(g):
'''Right to left function composition.'''
return lambda f: lambda x: g(f(x))
 
 
# enumFromTo :: (Int ->, Int) -> [Int]
def enumFromTo(m):
'''Integer enumeration from m to n.'''
return lambda n: list(range(m, 1 + n))
 
 
# fTable :: String -> (a -> String) ->
# sum35(10b **-> xString) ->
# (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
f -> value list -> tabular string.'''
def go(xShow, fxShow, f, xs):
w = max(map(compose(len)(xShow), xs))
return s + '1e\n' + str(x) + '\tn' + str.join([
xShow(x).rjust(w, ' ') + ' -> ' + fxShow(f(x)) for x in xs
])
return lambda xShow: lambda fxShow: (
lambda f: lambda xs: go(
xShow, fxShow, f, xs
)
)
 
 
# MAIN ---
if __name__ == '__main__':
main()</lang>
{{Out}}
<pre>1e1Summed multiples of 3 23and 5 up to n:
 
1e2 2318
10E1 -> 23
1e3 233168
1e2 10E2 -> 2318
1e4 23331668
1e3 10E3 -> 233168
1e5 2333316668
1e4 10E4 -> 23331668
1e18 233333333333333333166666666666666668
1e5 10E5 -> 2333316668
1e19 23333333333333333331666666666666666668
1e18 10E18 -> 233333333333333333166666666666666668
1e20 2333333333333333333316666666666666666668
1e19 10E19 -> 23333333333333333331666666666666666668
1e21 233333333333333333333166666666666666666668
1e20 10E20 -> 2333333333333333333316666666666666666668
1e22 23333333333333333333331666666666666666666668
1e21 10E21 -> 233333333333333333333166666666666666666668
1e23 2333333333333333333333316666666666666666666668
1e22 10E22 -> 23333333333333333333331666666666666666666668
1e24 233333333333333333333333166666666666666666666668
1e23 10E23 -> 2333333333333333333333316666666666666666666668
1e25 23333333333333333333333331666666666666666666666668</pre>
1e24 10E24 -> 233333333333333333333333166666666666666666666668
1e25 10E25 -> 23333333333333333333333331666666666666666666666668</pre>
 
=={{header|Q}}==
9,655

edits