Nth root: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: Small reduction.) |
m (→{{header|Haskell}}: Minor update to format of output.) |
||
Line 1,492: | Line 1,492: | ||
(((,) <*> ((/ n) . ((+) . ((n - 1) *) <*> (x /) . (** (n - 1))))) . snd) |
(((,) <*> ((/ n) . ((+) . ((n - 1) *) <*> (x /) . (** (n - 1))))) . snd) |
||
(x, x / n) |
(x, x / n) |
||
-- |
-------------------------- TESTS -------------------------- |
||
main :: IO () |
main :: IO () |
||
main = |
main = |
||
Line 1,503: | Line 1,504: | ||
(uncurry nthRoot) |
(uncurry nthRoot) |
||
[(2, 2), (5, 34), (10, 734 ^ 10), (0.5, 7)] |
[(2, 2), (5, 34), (10, 734 ^ 10), (0.5, 7)] |
||
-- |
-------------------- FORMAT OF RESULTS -------------------- |
||
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String |
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String |
||
fTable s xShow fxShow f xs = |
fTable s xShow fxShow f xs = |
||
Line 1,510: | Line 1,512: | ||
rjust n c = drop . length <*> (replicate n c ++) |
rjust n c = drop . length <*> (replicate n c ++) |
||
in unlines $ |
in unlines $ |
||
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs</lang> |
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs</lang> |
||
{{Out}} |
{{Out}} |
||
<pre>Nth roots: |
<pre>Nth roots: |
||
2.0 `nthRoot` 2.0 -> 1.414213562373095 |
2.0 `nthRoot` 2.0 -> 1.414213562373095 |
||
5.0 `nthRoot` 34.0 -> 2.0243974584998847 |
5.0 `nthRoot` 34.0 -> 2.0243974584998847 |
||
10.0 `nthRoot` 4.539004352165717e28 -> 734.0 |
10.0 `nthRoot` 4.539004352165717e28 -> 734.0 |
||
0.5 `nthRoot` 7.0 -> 49.0</pre> |
0.5 `nthRoot` 7.0 -> 49.0</pre> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |