Bernoulli numbers: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Light tidying of code and output in existing example)
Line 1,172:
The implementation of the algorithm is in the function bernoullis. The rest is for printing the results.
 
<lang Haskell>moduleimport Main whereData.Ratio
 
import Data.Ratio
import System.Environment
 
main = getArgs >>= printM . defaultArg where
where
defaultArg as = if null as then 60 else read (head as)
defaultArg as =
if null as
then 60
defaultArg as = if null as then 60 else read (head as)
 
printM m =
printM m = mapM_ (putStrLn . printP) . takeWhile ((<= m).fst)
takeWhile ((<= m) . fst) . filter (\(_, b) -> b /= 0 % 1) . zip [0 ..] $ bernoullis
bernoullis
 
printP (i, r) =
printP (i,r) = "B(" ++ show i ++ ") = " ++ show (numerator r) ++ "/" ++ show (denominator r)
 
bernoullis = map head . iterate (ulli 1) . map berno $ enumFrom 0 where
where
berno i = 1 % (i + 1)
ulli _ [_] = []
ulli i (x:y:xs) = (i % 1) * (x - y) : ulli (i + 1) (y : xs) </lang>
{{outOut}}
</lang>
<pre>B(0) = 1/1
{{out}}
B(1) = 1/2
<pre>
B(02) = 1/16
B(14) = -1/230
B(26) = 1/642
B(48) = -1/30
B(610) =1 5/4266
B(812) = -1691/302730
B(1014) =5 7/666
B(1216) = -6913617/2730510
B(1418) =7 43867/6798
B(1620) = -3617174611/510330
B(1822) =43867 854513/798138
B(2024) = -174611236364091/3302730
B(2226) =854513 8553103/1386
B(2428) = -23636409123749461029/2730870
B(30) = 8615841276005/14322
B(26)=8553103/6
B(2832) = -237494610297709321041217/870510
B(34) = 2577687858367/6
B(30)=8615841276005/14322
B(36) = -26315271553053477373/1919190
B(32)=-7709321041217/510
B(3438) =2577687858367 2929993913841559/6
B(40) = -261082718496449122051/13530
B(36)=-26315271553053477373/1919190
B(42) = 1520097643918070802691/1806
B(38)=2929993913841559/6
B(44) = -27833269579301024235023/690
B(40)=-261082718496449122051/13530
B(46) = 596451111593912163277961/282
B(42)=1520097643918070802691/1806
B(48) = -5609403368997817686249127547/46410
B(44)=-27833269579301024235023/690
B(50) = 495057205241079648212477525/66
B(46)=596451111593912163277961/282
B(52) = -801165718135489957347924991853/1590
B(48)=-5609403368997817686249127547/46410
B(54) = 29149963634884862421418123812691/798
B(50)=495057205241079648212477525/66
B(56) = -2479392929313226753685415739663229/870
B(52)=-801165718135489957347924991853/1590
B(58) = 84483613348880041862046775994036021/354
B(54)=29149963634884862421418123812691/798
B(60) = -1215233140483755572040304994079820246041491/56786730</pre>
B(56)=-2479392929313226753685415739663229/870
B(58)=84483613348880041862046775994036021/354
B(60)=-1215233140483755572040304994079820246041491/56786730
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==