hw1: impl multiply, power of
This commit is contained in:
@@ -63,7 +63,7 @@ bigSubtract' (x:xs) [] b = let d = x - b
|
|||||||
else d : bigSubtract' xs [] 0
|
else d : bigSubtract' xs [] 0
|
||||||
|
|
||||||
bigEq :: BigNum -> BigNum -> Bool
|
bigEq :: BigNum -> BigNum -> Bool
|
||||||
bigEq x y = stripLeadingZeroes x == stripLeadingZeroes y
|
bigEq x y = stripLeadingZeroes (reverse x) == stripLeadingZeroes (reverse y)
|
||||||
|
|
||||||
bigDec :: BigNum -> BigNum
|
bigDec :: BigNum -> BigNum
|
||||||
bigDec x = bigSubtract x [1]
|
bigDec x = bigSubtract x [1]
|
||||||
@@ -73,10 +73,25 @@ bigDec x = bigSubtract x [1]
|
|||||||
-- If you are having trouble finding a solution, write a helper method that
|
-- If you are having trouble finding a solution, write a helper method that
|
||||||
-- multiplies a BigNum by an Int.
|
-- multiplies a BigNum by an Int.
|
||||||
bigMultiply :: BigNum -> BigNum -> BigNum
|
bigMultiply :: BigNum -> BigNum -> BigNum
|
||||||
bigMultiply _ _ = error "Your code here"
|
bigMultiply x y = reverse $ stripLeadingZeroes $ reverse $ bigMultiply' x y
|
||||||
|
|
||||||
|
bigMultiply' :: BigNum -> BigNum -> BigNum
|
||||||
|
bigMultiply' _ [] = []
|
||||||
|
bigMultiply' x (y:ys) = bigAdd (bigMultiplyBlock x y) (bigMultiply (0:x) ys)
|
||||||
|
|
||||||
|
bigMultiplyBlock :: BigNum -> Block -> BigNum
|
||||||
|
bigMultiplyBlock x y = bigMultiplyBlock' x y 0
|
||||||
|
|
||||||
|
bigMultiplyBlock' :: BigNum -> Block -> Block -> BigNum
|
||||||
|
bigMultiplyBlock' [] _ 0 = []
|
||||||
|
bigMultiplyBlock' [] _ c = [c]
|
||||||
|
bigMultiplyBlock' (x:xs) y c = let p = x * y + c
|
||||||
|
in (p `mod` maxblock) : bigMultiplyBlock' xs y (p `div` maxblock)
|
||||||
|
|
||||||
bigPowerOf :: BigNum -> BigNum -> BigNum
|
bigPowerOf :: BigNum -> BigNum -> BigNum
|
||||||
bigPowerOf _ _ = error "Your code here"
|
bigPowerOf x [0] = [1]
|
||||||
|
bigPowerOf x [1] = x
|
||||||
|
bigPowerOf x y = bigMultiply x (bigPowerOf x (bigDec y))
|
||||||
|
|
||||||
prettyPrint :: BigNum -> String
|
prettyPrint :: BigNum -> String
|
||||||
prettyPrint [] = ""
|
prettyPrint [] = ""
|
||||||
|
|||||||
13
hw1/output
13
hw1/output
@@ -10,4 +10,17 @@ Subtraction
|
|||||||
[1]
|
[1]
|
||||||
[0]
|
[0]
|
||||||
Multiplication
|
Multiplication
|
||||||
|
[12]
|
||||||
|
[0]
|
||||||
|
[392,296,4,12]
|
||||||
|
[518,250,645,161,37,915,479,1]
|
||||||
|
Power Of
|
||||||
|
[256]
|
||||||
|
[1]
|
||||||
|
Others
|
||||||
7
|
7
|
||||||
|
63
|
||||||
|
256
|
||||||
|
800,000,000,000,000,000,002
|
||||||
|
1
|
||||||
|
59,745,672,527,794,294,248,045
|
||||||
|
|||||||
Reference in New Issue
Block a user