Last active
December 26, 2015 23:39
-
-
Save bryant/7231662 to your computer and use it in GitHub Desktop.
binary exponentiation. haskell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import System.Environment (getArgs) | |
myPow x y | |
| y < 0 = error "neg." | |
| y == 0 = 1 | |
| otherwise = pow' y x | |
where | |
pow' kth cur | |
| kth == 1 = cur | |
| odd kth = cur * pow' (kth `quot` 2) (cur * cur) | |
| otherwise = pow' (kth `quot` 2) (cur * cur) | |
main = do | |
ints <- fmap (map read) $ getArgs :: IO [Int] | |
print $ (head ints) `myPow` (last ints) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment