Feigenbaum constant calculation: Difference between revisions
Content deleted Content added
→{{header|Haskell}}: Added a Haskell version |
|||
Line 196: | Line 196: | ||
13 4.66920537 |
13 4.66920537 |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
|||
<lang haskell>import Data.List (mapAccumL, foldl') --' strict |
|||
feigenbaumApprox :: Int -> [Double] |
|||
feigenbaumApprox mx = snd $ mitch mx 10 |
|||
where |
|||
mitch :: Int -> Int -> ((Double, Double, Double), [Double]) |
|||
mitch mx mxj = |
|||
mapAccumL |
|||
(\(a1, a2, d1) i -> |
|||
let a = |
|||
foldl' --' strict variant of foldl |
|||
(\acc v -> |
|||
let (x, y) = |
|||
iterate |
|||
(\(p, q) -> |
|||
(acc - (p * p), 1.0 - ((2.0 * p) * q))) |
|||
(0.0, 0.0) !! |
|||
(2 ^ i) |
|||
in acc - (x / y)) |
|||
(a1 + (a1 - a2) / d1) |
|||
[1 .. mxj] |
|||
d = (a1 - a2) / (a - a1) |
|||
in ((a, a1, d), d)) |
|||
(1.0, 0.0, 3.2) |
|||
(take mx [2 ..]) |
|||
main :: IO () |
|||
main = |
|||
(putStrLn . unlines) $ |
|||
zipWith |
|||
(\i s -> justifyRight 2 ' ' (show i) ++ '\t' : s) |
|||
[1 ..] |
|||
(show <$> feigenbaumApprox 13) |
|||
where |
|||
justifyRight n c s = drop (length s) (replicate n c ++ s)</lang> |
|||
{{Out}} |
|||
<pre> 1 3.2185114220380866 |
|||
2 4.3856775985683365 |
|||
3 4.600949276538056 |
|||
4 4.6551304953919646 |
|||
5 4.666111947822846 |
|||
6 4.668548581451485 |
|||
7 4.66906066077106 |
|||
8 4.669171554514976 |
|||
9 4.669195154039278 |
|||
10 4.669200256503637 |
|||
11 4.669200975097843 |
|||
12 4.669205372040318 |
|||
13 4.669207514010413</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |