Faulhaber's triangle: Difference between revisions

m
→‎{{header|Haskell}}: Formatting lint, and a salute to Spoon!'s helpful Faulhaber edits
(→‎{{header|Haskell}}: simpler base case)
m (→‎{{header|Haskell}}: Formatting lint, and a salute to Spoon!'s helpful Faulhaber edits)
Line 42:
 
-- FAULHABER -------------------------------------------------------------------
 
-- Infinite list of rows of Faulhaber's triangle
faulhaberTriangle :: [[Rational]]
faulhaberTriangle = tail $ scanl fh_ [] [0 ..]
where
where fh_ :: [Rational] -> Integer -> [Rational]
fh_ x:: n[Rational] =-> (1Integer -> sum ys) : ys[Rational]
fh_ x where ysn = zipWith (\nd j1 -> ndsum * (n % (j + 2))ys) x: [0 ..]ys
where
faulhaber p n = sum $ ys = zipWith (\nd ij -> nd * (n ^ i))% (faulhaberTrianglej !!+ p2))) x [10 ..]
 
-- p -> n -> Sum of the p-th powers of the first n positive integers
faulhaber :: Int -> Rational -> Rational
faulhaber p n =
faulhaber p n = sum $ zipWith (\nd i -> nd * (n ^ i)) (faulhaberTriangle !! p) [1 ..]
sum $ zipWith (\nd i -> nd * (n ^ i)) (faulhaberTriangle !! p) [1 ..]
 
 
-- DISPLAY ---------------------------------------------------------------------
 
-- (Max numerator+denominator widths) -> Column width -> Filler -> Ratio -> String
justifyRatio :: (Int, Int) -> Int -> Char -> Rational -> String
Line 78:
-- List of Ratios -> (Max numerator width, Max denominator width)
maxWidths :: [[Rational]] -> (Int, Int)
maxWidths xss = (n, d)
where
let widest f xs = maximum $ fmap (length . show . f) xs
[n, d] = [widest numerator, widest denominator] <*> [concat xss]
in (n, d)
 
 
-- TEST ------------------------------------------------------------------------
9,655

edits