Faulhaber's triangle: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: Pruned one import, updated primitives, tidied.)
Line 2,267: Line 2,267:
<lang python>'''Faulhaber's triangle'''
<lang python>'''Faulhaber's triangle'''


from itertools import (accumulate, chain, count, islice, starmap)
from itertools import accumulate, chain, count, islice
from fractions import (Fraction)
from fractions import Fraction




Line 2,275: Line 2,275:
'''List of rows of Faulhaber fractions.'''
'''List of rows of Faulhaber fractions.'''
def go(rs, n):
def go(rs, n):
xs = list(starmap(
def f(x, y):
lambda x, y: Fraction(n, x) * y,
return Fraction(n, x) * y
zip(islice(count(2), m), rs)
xs = list(map(f, islice(count(2), m), rs))
))
return [Fraction(1 - sum(xs), 1)] + xs
return [Fraction(1 - sum(xs), 1)] + xs

return list(accumulate(
return list(accumulate(
[[]] + list(islice(count(0), 1 + m)),
[[]] + list(islice(count(0), 1 + m)),
Line 2,291: Line 2,291:
positive integers.
positive integers.
'''
'''
def go(x, y):
return y * (n ** x)

return sum(
return sum(
map(go, count(1), faulhaberTriangle(p)[-1])
list(starmap(
lambda x, y: y * (n ** x),
zip(count(1), faulhaberTriangle(p)[-1])
))
)
)




# TEST ----------------------------------------------------
# ------------------------- TEST -------------------------
def main():
def main():
'''Tests'''
'''Tests'''
Line 2,319: Line 2,319:




# DISPLAY -------------------------------------------------
# ----------------------- DISPLAY ------------------------


# fTable :: String -> (a -> String) ->
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
def fTable(s):
'''Heading -> x display function -> fx display function ->
'''Heading -> x display function ->
f -> xs -> tabular string.
fx display function -> f -> xs -> tabular string.
'''
'''
def go(xShow, fxShow, f, xs):
def gox(xShow):
ys = [xShow(x) for x in xs]
def gofx(fxShow):
w = max(map(len, ys))
def gof(f):
return s + '\n' + '\n'.join(map(
def goxs(xs):
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
ys = [xShow(x) for x in xs]
xs, ys
w = max(map(len, ys))

))
def arrowed(x, y):
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
return y.rjust(w, ' ') + ' -> ' + (
xShow, fxShow, f, xs
fxShow(f(x))
)
)
return s + '\n' + '\n'.join(
map(arrowed, xs, ys)
)
return goxs
return gof
return gofx
return gox




# GENERIC -------------------------------------------------
# ----------------------- GENERIC ------------------------


# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
Line 2,351: Line 2,359:
def concat(xs):
def concat(xs):
'''The concatenation of all the elements
'''The concatenation of all the elements
in a list or iterable.'''
in a list or iterable.
'''
def f(ys):
def f(ys):
zs = list(chain(*ys))
zs = list(chain(*ys))
Line 2,368: Line 2,377:
f lifted to a function over a list.
f lifted to a function over a list.
'''
'''
return lambda xs: list(map(f, xs))
def go(xs):
return list(map(f, xs))

return go




Line 2,386: Line 2,398:
representation of the ratio r.
representation of the ratio r.
'''
'''
def go(n, r):
def go(n):
d = r.denominator
def f(r):
return str(r.numerator).rjust(m, ' ') + (
d = r.denominator
('/' + str(d).ljust(n, ' ')) if 1 != d else (
return str(r.numerator).rjust(m, ' ') + (
' ' * (1 + n)
('/' + str(d).ljust(n, ' ')) if 1 != d else (
' ' * (1 + n)
)
)
)
)
return f
return lambda n: lambda r: go(n, r)
return go