hw1: impl multiply, power of

This commit is contained in:
2026-02-14 23:58:43 -08:00
parent b9e10c3e37
commit fa77c90d1b
2 changed files with 31 additions and 3 deletions

View File

@@ -63,7 +63,7 @@ bigSubtract' (x:xs) [] b = let d = x - b
else d : bigSubtract' xs [] 0
bigEq :: BigNum -> BigNum -> Bool
bigEq x y = stripLeadingZeroes x == stripLeadingZeroes y
bigEq x y = stripLeadingZeroes (reverse x) == stripLeadingZeroes (reverse y)
bigDec :: BigNum -> BigNum
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
-- multiplies a BigNum by an Int.
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 _ _ = 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 [] = ""

View File

@@ -10,4 +10,17 @@ Subtraction
[1]
[0]
Multiplication
[12]
[0]
[392,296,4,12]
[518,250,645,161,37,915,479,1]
Power Of
[256]
[1]
Others
7
63
256
800,000,000,000,000,000,002
1
59,745,672,527,794,294,248,045