Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
1
Task/Nth-root/Haskell/nth-root-1.hs
Normal file
1
Task/Nth-root/Haskell/nth-root-1.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
n `nthRoot` x = fst $ until (uncurry(==)) (\(_,x0) -> (x0,((n-1)*x0+x/x0**(n-1))/n)) (x,x/n)
|
||||
28
Task/Nth-root/Haskell/nth-root-2.hs
Normal file
28
Task/Nth-root/Haskell/nth-root-2.hs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
nthRoot :: Double -> Double -> Double
|
||||
nthRoot n x =
|
||||
fst $
|
||||
until
|
||||
(uncurry (==))
|
||||
(((,) <*> ((/ n) . ((+) . (pn *) <*> (x /) . (** pn)))) . snd)
|
||||
(x, x / n)
|
||||
where
|
||||
pn = pred n
|
||||
|
||||
-------------------------- TESTS --------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
putStrLn $
|
||||
fTable
|
||||
"Nth roots:"
|
||||
(\(a, b) -> show a ++ " `nthRoot` " ++ show b)
|
||||
show
|
||||
(uncurry nthRoot)
|
||||
[(2, 2), (5, 34), (10, 734 ^ 10), (0.5, 7)]
|
||||
|
||||
-------------------- FORMAT OF RESULTS --------------------
|
||||
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
|
||||
fTable s xShow fxShow f xs =
|
||||
let w = maximum (length . xShow <$> xs)
|
||||
rjust n c = drop . length <*> (replicate n c ++)
|
||||
in unlines $
|
||||
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs
|
||||
Loading…
Add table
Add a link
Reference in a new issue