Faulhaber's triangle: Difference between revisions

m
→‎{{header|Python}}: Pruned one import, updated primitives, tidied.
m (→‎{{header|Python}}: Pruned one import, updated primitives, tidied.)
Line 2,267:
<lang python>'''Faulhaber's triangle'''
 
from itertools import (accumulate, chain, count, islice, starmap)
from fractions import (Fraction)
 
 
Line 2,275:
'''List of rows of Faulhaber fractions.'''
def go(rs, n):
xsdef =f(x, list(starmap(y):
lambda x, y:return Fraction(n, x) * y,
xs = ziplist(map(f, islice(count(2), m), rs))
))
return [Fraction(1 - sum(xs), 1)] + xs
 
return list(accumulate(
[[]] + list(islice(count(0), 1 + m)),
Line 2,291:
positive integers.
'''
def go(x, y):
lambda x, y:return y * (n ** x),
 
return sum(
map(go, zip(count(1), faulhaberTriangle(p)[-1])
list(starmap(
lambda x, y: y * (n ** x),
zip(count(1), faulhaberTriangle(p)[-1])
))
)
 
 
# TEST --------------------------- TEST -------------------------
def main():
'''Tests'''
Line 2,319:
 
 
# DISPLAY ------------------------- DISPLAY ------------------------
 
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
fx display function -> f -> xs -> tabular string.
'''
def gogox(xShow, fxShow, f, xs):
ysdef = [xShowgofx(xfxShow) for x in xs]:
w = max(map(len, ys) def gof(f):
return s + '\n' + '\n'.join(map def goxs(xs):
lambda x, y: y.rjust(w, ' ') + ' ->ys '= + fxShow(f[xShow(x)), for x in xs]
xs 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
Line 2,351 ⟶ 2,359:
def concat(xs):
'''The concatenation of all the elements
in a list or iterable.'''
)'''
def f(ys):
zs = list(chain(*ys))
Line 2,368 ⟶ 2,377:
f lifted to a function over a list.
'''
returndef lambda xs: listgo(map(f, xs)):
return list(map(f, xs))
 
return go
 
 
Line 2,386 ⟶ 2,398:
representation of the ratio r.
'''
def go(n, r):
d =def f(r.denominator):
return str(r.numerator).rjust(m, ' ') +d (= r.denominator
('/' +return str(dr.numerator).ljustrjust(nm, ' ')) if 1 != d else+ (
(' /' *+ str(1d).ljust(n, +' n')) if 1 != d else (
' ' * (1 + n)
) )
)
)return f
return lambda n: lambda r: go(n, r)
 
 
9,655

edits