Modular exponentiation: Difference between revisions
Content deleted Content added
Frink |
→{{header|Haskell}}: Added mod=1 case and improved readability by moving final expression into the pattern match and introducing where variables |
||
Line 876: | Line 876: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<lang haskell> |
|||
modPow :: Integer -> Integer -> Integer -> Integer -> Integer |
|||
⚫ | |||
modPow b e 1 r = 0 |
|||
⚫ | |||
⚫ | |||
modPow b e m r |
|||
powm b e m r = powm (b * b `mod` m) (e `div` 2) m r |
|||
⚫ | |||
| otherwise = modPow b' e' m r |
|||
where |
|||
b' = b * b `mod` m |
|||
e' = e `div` 2 |
|||
main |
main = do |
||
⚫ | |||
main = |
|||
print $ |
|||
powm |
|||
⚫ | |||
2351399303373464486466122544523690094744975233415544072992656881240319 |
2351399303373464486466122544523690094744975233415544072992656881240319 |
||
(10 ^ 40) |
(10 ^ 40) |
||
1 |
1) |
||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>1527229998585248450016808958343740453059</pre> |
<pre>1527229998585248450016808958343740453059</pre> |