aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-03-30 02:40:13 +0100
committerGitHub <noreply@github.com>2021-03-30 02:40:13 +0100
commitb423c085bee9f9ff4f53264dc15e3c015d1913fc (patch)
tree0855e70564886c1109d1eb98481be7d7157d30fb
parent5d20a484ac4e9d0b7c0e048ed973084329d555de (diff)
parenteae9faa071c9006df4fa78153f387b4de0c01486 (diff)
downloadperlweeklychallenge-club-b423c085bee9f9ff4f53264dc15e3c015d1913fc.tar.gz
perlweeklychallenge-club-b423c085bee9f9ff4f53264dc15e3c015d1913fc.tar.bz2
perlweeklychallenge-club-b423c085bee9f9ff4f53264dc15e3c015d1913fc.zip
Merge pull request #3805 from stuart-little/stuart-little_106_haskell
1st commit on 106_haskell
-rwxr-xr-xchallenge-106/stuart-little/haskell/ch-1.hs15
-rwxr-xr-xchallenge-106/stuart-little/haskell/ch-2.hs35
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-106/stuart-little/haskell/ch-1.hs b/challenge-106/stuart-little/haskell/ch-1.hs
new file mode 100755
index 0000000000..e32cfdfcef
--- /dev/null
+++ b/challenge-106/stuart-little/haskell/ch-1.hs
@@ -0,0 +1,15 @@
+#!/usr/bin/env runghc
+
+-- run <script> <space-separated numbers>
+
+import Data.List (sort)
+import Safe (maximumDef)
+import System.Environment (getArgs)
+
+maxGap :: [Int] -> Int
+maxGap xs = maximumDef 0 $ map (\x -> snd x - fst x) $ zip sorted $ tail sorted where
+ sorted = sort xs
+
+main = do
+ nrs <- getArgs >>= return . map (read::String->Int)
+ print $ maxGap nrs
diff --git a/challenge-106/stuart-little/haskell/ch-2.hs b/challenge-106/stuart-little/haskell/ch-2.hs
new file mode 100755
index 0000000000..4d05cd4c27
--- /dev/null
+++ b/challenge-106/stuart-little/haskell/ch-2.hs
@@ -0,0 +1,35 @@
+#!/usr/bin/env runghc
+
+-- run <script> <numerator> <denominator>
+
+import System.Environment (getArgs)
+
+maxExp :: Int -> Int -> Int
+maxExp p n
+ |mod n p /= 0 =0
+ |otherwise = succ $ maxExp p $ div n p
+
+ordExp :: Int -> Int -> Int
+ordExp n p = fst $ head $ dropWhile (\x -> mod ((snd x)-1) p /= 0) $ zip [1..] $ iterate (\x -> mod (x*n) p) (mod n p)
+
+when10copr :: Int -> Int -> (Int,String)
+when10copr num den = (intg, (replicate (digs - (length $ show fract)) '0') ++ (show fract)) where
+ intg = div num den
+ digs = ordExp 10 den
+ fract = div ((mod num den) * (10^digs-1)) den
+
+fractPart :: Int -> Int -> String
+fractPart num den = nonRep ++ rep where
+ exp2 = maxExp 2 den
+ exp5 = maxExp 5 den
+ newNum = if (exp2 >= exp5) then (5^(exp2-exp5) * num) else (2^(exp5-exp2) * num)
+ copr = when10copr newNum $ div den (2^exp2 * 5^exp5)
+ nonRep = if ((fst copr) + (max exp2 exp5) > 0) then (replicate ((max exp2 exp5) - (length $ show $ fst copr)) '0') ++ (show $ fst copr) else ""
+ rep = if (snd copr /= "0") then ("(" ++ snd copr ++ ")") else ""
+
+fractTot :: Int -> Int -> String
+fractTot num den = (show $ div num den) ++ "." ++ fractPart (mod num den) den
+
+main = do
+ (num:den:_) <- getArgs >>= return . map (read::String -> Int)
+ putStrLn $ fractTot num den