Bernoulli numbers: Difference between revisions
Content deleted Content added
→{{header|Haskell}}: Light tidying of code and output in existing example |
→{{header|Haskell}}: Alternative version |
||
Line 1,168: | Line 1,168: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
====Task algorithm==== |
|||
This program works as a command line utility, that reads from stdin the number of elements to compute (default 60) and prints them in stdout. |
This program works as a command line utility, that reads from stdin the number of elements to compute (default 60) and prints them in stdout. |
||
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. |
||
Line 1,197: | Line 1,197: | ||
{{Out}} |
{{Out}} |
||
<pre>B(0) = 1/1 |
<pre>B(0) = 1/1 |
||
B(1) = 1/2 |
|||
B(2) = 1/6 |
|||
B(4) = -1/30 |
|||
B(6) = 1/42 |
|||
B(8) = -1/30 |
|||
B(10) = 5/66 |
|||
B(12) = -691/2730 |
|||
B(14) = 7/6 |
|||
B(16) = -3617/510 |
|||
B(18) = 43867/798 |
|||
B(20) = -174611/330 |
|||
B(22) = 854513/138 |
|||
B(24) = -236364091/2730 |
|||
B(26) = 8553103/6 |
|||
B(28) = -23749461029/870 |
|||
B(30) = 8615841276005/14322 |
|||
B(32) = -7709321041217/510 |
|||
B(34) = 2577687858367/6 |
|||
B(36) = -26315271553053477373/1919190 |
|||
B(38) = 2929993913841559/6 |
|||
B(40) = -261082718496449122051/13530 |
|||
B(42) = 1520097643918070802691/1806 |
|||
B(44) = -27833269579301024235023/690 |
|||
B(46) = 596451111593912163277961/282 |
|||
B(48) = -5609403368997817686249127547/46410 |
|||
B(50) = 495057205241079648212477525/66 |
|||
B(52) = -801165718135489957347924991853/1590 |
|||
B(54) = 29149963634884862421418123812691/798 |
|||
B(56) = -2479392929313226753685415739663229/870 |
|||
B(58) = 84483613348880041862046775994036021/354 |
|||
B(60) = -1215233140483755572040304994079820246041491/56786730</pre> |
|||
====Derivation from Faulhaber's triangle==== |
|||
<lang haskell>import Data.Ratio (Ratio, numerator, denominator, (%)) |
|||
bernouilliNumbers :: Integer -> [Rational] |
|||
bernouilliNumbers = |
|||
fmap head . |
|||
tail . |
|||
scanl |
|||
(\rs n -> |
|||
let xs = zipWith ((*) . (n %)) [2 ..] rs |
|||
in 1 - sum xs : xs) |
|||
[] . |
|||
enumFromTo 0 |
|||
main :: IO () |
|||
main = |
|||
(putStrLn . unlines) |
|||
(concat $ |
|||
zipWith |
|||
(\i x -> |
|||
let n = numerator x |
|||
in [ concat ["B(", show i, ") = ", show n, "/", show (denominator x)] |
|||
| n /= 0 ]) |
|||
[0 ..] |
|||
(bernouilliNumbers 60))</lang> |
|||
{{Out}} |
|||
<pre> |
|||
maybe Monad |
|||
identity.scpt |
|||
js-001.scpt |
|||
js-002.scpt |
|||
js-003.scpt |
|||
js-004.scpt |
|||
jsMaybe.scpt |
|||
maybeAS-001.scpt |
|||
maybeFunction-001.scpt |
|||
maybeJS-005.scpt |
|||
setComprehension.scpt |
|||
setQuery-001.scpt |
|||
maybeFunction-001.scpt |
|||
TP2OG-022.scpt |
|||
TP2OG-024.scpt |
|||
bitPopulationJS-002.scpt |
|||
Untitled |
|||
Main.hs |
|||
tmp.hs |
|||
Settings |
|||
faulHaberES6-010.scpt |
|||
1 |
|||
13 |
|||
14 |
|||
15 |
|||
16 |
|||
17 |
|||
18 |
|||
19 |
|||
20 |
|||
21 |
|||
22 |
|||
23 |
|||
24 |
|||
7 |
|||
8 |
|||
9 |
|||
10 |
|||
11 |
|||
12 |
|||
1 |
|||
2 |
|||
3 |
|||
4 |
|||
5 |
|||
6 |
|||
main :: IO () |
|||
main = |
|||
(putStrLn . unlines) |
|||
(concat $ |
|||
zipWith |
|||
(\i x -> |
|||
let n = numerator x |
|||
in [ concat ["B(", show i, ") = ", show n, "/", show (denominator x)] |
|||
| n /= 0 ]) |
|||
[0 ..] |
|||
(bernouilliNumbers 60)) |
|||
scanl |
|||
(\rs n -> |
|||
let xs = zipWith ((*) . (n %)) [2 ..] rs |
|||
in 1 - sum xs : xs) |
|||
[] . |
|||
enumFromTo 0 |
|||
import Data.Ratio (Ratio, numerator, denominator, (%)) |
|||
bernouilliNumbers :: Integer -> [Rational] |
|||
bernouilliNumbers = |
|||
fmap head . |
|||
tail . |
|||
Haskell - Main.hs:22 |
|||
B(0) = 1/1 |
|||
B(1) = 1/2 |
B(1) = 1/2 |
||
B(2) = 1/6 |
B(2) = 1/6 |