Bernoulli numbers: Difference between revisions

m
Haskell :: Derivation from Faulhaber's triangle – Tidied
(Marked as incorrect.)
m (Haskell :: Derivation from Faulhaber's triangle – Tidied)
Line 1,797:
 
====Derivation from Faulhaber's triangle====
<lang haskell>import Data.RatioBool (Ratio, numerator, denominator, (%)bool)
import Data.BoolRatio (boolRatio, denominator, numerator, (%))
 
-------------------- BERNOULLI NUMBERS -------------------
 
bernouillis :: Integer -> [Rational]
bernouillis =
bernouillis = fmap head . tail . scanl faulhaber [] . enumFromTo 0
fmap head
. tail
. scanl faulhaber []
. enumFromTo 0
 
faulhaber :: [Ratio Integer] -> Integer -> [Ratio Integer]
faulhaber rs n = (:) =<< (-) 1 . sum $ zipWith ((*) . (n %)) [2 ..] rs
(:) =<< (-) 1 . sum $
zipWith ((*) . (n %)) [2 ..] rs
 
-- TEST -------------------------- TEST -------------------------
main :: IO ()
main = do
Line 1,819 ⟶ 1,827:
(filter ((0 /=) . snd) $ zip [0 ..] xs)
 
-- FORMATTING ----------------------- FORMATTING ----------------------
fTable ::
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
String ->
(a -> String) ->
(b -> String) ->
(a -> b) ->
[a] ->
String
fTable s xShow fxShow f xs =
let w = maximum (length . xShow <$> xs)
in unlines $
s :
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs
fmap
( ((<>) . rjust w ' ' . xShow)
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++<>) . fxShow . f)) xs
)
xs
 
showRatio :: Int -> Rational -> String
showRatio w r =
let d = denominator r
in rjust w ' ' ($ show (numerator r)) ++ bool [] (" / " ++ show d) (1 /= d)
<> bool [] (" / " <> show d) (1 /= d)
 
rjust :: Int -> a -> [a] -> [a]
rjust n c = drop . length <*> (replicate n c ++<>)</lang>
{{Out}}
<pre>Bernouillis from Faulhaber triangle:
9,655

edits