Bernoulli numbers: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Light tidying of code and output in existing example) |
|||
Line 1,172: | Line 1,172: | ||
The implementation of the algorithm is in the function bernoullis. The rest is for printing the results. |
The implementation of the algorithm is in the function bernoullis. The rest is for printing the results. |
||
<lang Haskell> |
<lang Haskell>import Data.Ratio |
||
import Data.Ratio |
|||
import System.Environment |
import System.Environment |
||
main = getArgs >>= |
main = getArgs >>= printM . defaultArg |
||
where |
|||
⚫ | |||
defaultArg as = |
|||
if null as |
|||
then 60 |
|||
⚫ | |||
printM m = |
|||
mapM_ (putStrLn . printP) . |
|||
takeWhile ((<= m) . fst) . filter (\(_, b) -> b /= 0 % 1) . zip [0 ..] $ |
|||
bernoullis |
|||
printP (i, r) = |
|||
"B(" ++ show i ++ ") = " ++ show (numerator r) ++ "/" ++ show (denominator r) |
|||
bernoullis = |
bernoullis = map head . iterate (ulli 1) . map berno $ enumFrom 0 |
||
where |
|||
berno i = 1 % (i+1) |
berno i = 1 % (i + 1) |
||
ulli _ [_] = [] |
ulli _ [_] = [] |
||
ulli i (x:y:xs) = (i%1)*(x-y) : ulli (i+1) (y:xs) |
ulli i (x:y:xs) = (i % 1) * (x - y) : ulli (i + 1) (y : xs)</lang> |
||
⚫ | |||
</lang> |
|||
<pre>B(0) = 1/1 |
|||
⚫ | |||
B(1) = 1/2 |
|||
<pre> |
|||
B( |
B(2) = 1/6 |
||
B( |
B(4) = -1/30 |
||
B( |
B(6) = 1/42 |
||
B( |
B(8) = -1/30 |
||
B( |
B(10) = 5/66 |
||
B( |
B(12) = -691/2730 |
||
B( |
B(14) = 7/6 |
||
B( |
B(16) = -3617/510 |
||
B( |
B(18) = 43867/798 |
||
B( |
B(20) = -174611/330 |
||
B( |
B(22) = 854513/138 |
||
B( |
B(24) = -236364091/2730 |
||
B( |
B(26) = 8553103/6 |
||
B( |
B(28) = -23749461029/870 |
||
⚫ | |||
B(26)=8553103/6 |
|||
B( |
B(32) = -7709321041217/510 |
||
B(34) = 2577687858367/6 |
|||
⚫ | |||
⚫ | |||
B(32)=-7709321041217/510 |
|||
B( |
B(38) = 2929993913841559/6 |
||
⚫ | |||
⚫ | |||
⚫ | |||
B(38)=2929993913841559/6 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</pre> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |