Skip to content

Commit b9ff087

Browse files
committed
documented functions, added fib
1 parent 8e89acc commit b9ff087

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Math.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
module Math where
22

3-
hcf :: Int -> Int -> Int
3+
fibs :: [Int] -- Fibonacci sequence
4+
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
5+
6+
fib :: Int -> Int -- Fibonacci number
7+
fib n = fibs !! n
8+
9+
hcf :: Int -> Int -> Int -- Highest common factor
410
hcf 0 y = y
511
hcf x y
612
| x < 0 = hcf (negate x) y
713
| x <= y = hcf x (y - x)
814
| otherwise = hcf y x
915

10-
lb :: Int -> Maybe Int
16+
lb :: Int -> Maybe Int -- Binary logarithm, rounded down
1117
lb 1 = Just 0
1218
lb x
1319
| x > 1 = fmap (+1) $ lb (div x 2)
1420
| otherwise = Nothing
1521

16-
lbRnd :: Int -> Maybe Int
22+
lbRnd :: Int -> Maybe Int -- Binary logarithm, rounded nearest
1723
lbRnd 1 = Just 0
1824
lbRnd x = let
1925
m :: Floating b => Int -> b
@@ -23,7 +29,7 @@ lbRnd x = let
2329
in
2430
if fmap ((fromIntegral x)<) (mid x) == Just True then lb x else lbMax x
2531

26-
lbMax :: Int -> Maybe Int
32+
lbMax :: Int -> Maybe Int -- Binary logarithm, rounded up
2733
lbMax 1 = Just 0
2834
lbMax x
2935
| mod x 2 == 0 = fmap (+1) $ lbMax (div x 2)

0 commit comments

Comments
 (0)