From 2a281ed2606e18459189a60cab9c18a659ab74e3 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Thu, 6 Feb 2014 21:13:46 +1100 Subject: Renamed remaining code from original fractran file more appropriately --- fractran.hs | 57 --------------------------------------------------------- misc.hs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 57 deletions(-) delete mode 100644 fractran.hs create mode 100644 misc.hs diff --git a/fractran.hs b/fractran.hs deleted file mode 100644 index 0d78ef9..0000000 --- a/fractran.hs +++ /dev/null @@ -1,57 +0,0 @@ - - - -isInt :: (RealFrac a) => a -> Bool -isInt x = - x == fromInteger (round x) - - - -modulo :: Int -> Int -> Int -modulo x y = - x - (x `div` y) * y - - - -primeFactors :: Int -> [Int] -primeFactors x = - let p = (\x e c -> if (x == 1) - then (reverse c) - else if (x `modulo` (head e) == 0) - then p (x `div` (head e)) e ((head e) : c) - else p x (tail e) c) - in p x euler [] - - - -euler :: [Int] -euler = - let f = (\list -> (head list) : (f (filter (\x -> x `modulo` (head list) /= 0) list))) - in f [2..] - - - -isPowerOf :: Int -> Int -> Bool -isPowerOf x y = - case (compare x y) of - LT -> False - EQ -> True - GT -> if (x `modulo` y == 0) then isPowerOf (x `div` y) y else False - - - -fractran :: [(Int,Int)] -> Int -> [Int] -fractran program value = - let prog = map (\(x,y) -> (fromIntegral x, fromIntegral y)) program - f = (\p v -> if (p == []) - then [] - else let (curX, curY) = head p - newV = v * curX / curY - in if (isInt newV) - then newV : (f prog newV) - else f (tail p) v) - result = map round (f prog (fromIntegral value)) - in value : result - - - diff --git a/misc.hs b/misc.hs new file mode 100644 index 0000000..c5bf33f --- /dev/null +++ b/misc.hs @@ -0,0 +1,40 @@ + + + +isInt :: (RealFrac a) => a -> Bool +isInt x = + x == fromInteger (round x) + + + +modulo :: Int -> Int -> Int +modulo x y = + x - (x `div` y) * y + + + +primeFactors :: Int -> [Int] +primeFactors x = + let p = (\x e c -> if (x == 1) + then (reverse c) + else if (x `modulo` (head e) == 0) + then p (x `div` (head e)) e ((head e) : c) + else p x (tail e) c) + in p x euler [] + + + +euler :: [Int] +euler = + let f = (\list -> (head list) : (f (filter (\x -> x `modulo` (head list) /= 0) list))) + in f [2..] + + + +isPowerOf :: Int -> Int -> Bool +isPowerOf x y = + case (compare x y) of + LT -> False + EQ -> True + GT -> if (x `modulo` y == 0) then isPowerOf (x `div` y) y else False + -- cgit