MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / _modexpt

Function _modexpt

project_euler/problem_188/sol1.py:23–42  ·  view source on GitHub ↗

Returns the modular exponentiation, that is the value of `base ** exponent % modulo_value`, without calculating the actual number. >>> _modexpt(2, 4, 10) 6 >>> _modexpt(2, 1024, 100) 16 >>> _modexpt(13, 65535, 7) 6

(base: int, exponent: int, modulo_value: int)

Source from the content-addressed store, hash-verified

21
22# small helper function for modular exponentiation (fast exponentiation algorithm)
23def _modexpt(base: int, exponent: int, modulo_value: int) -> int:
24 """
25 Returns the modular exponentiation, that is the value
26 of `base ** exponent % modulo_value`, without calculating
27 the actual number.
28 >>> _modexpt(2, 4, 10)
29 6
30 >>> _modexpt(2, 1024, 100)
31 16
32 >>> _modexpt(13, 65535, 7)
33 6
34 """
35
36 if exponent == 1:
37 return base
38 if exponent % 2 == 0:
39 x = _modexpt(base, exponent // 2, modulo_value) % modulo_value
40 return (x * x) % modulo_value
41 else:
42 return (base * _modexpt(base, exponent - 1, modulo_value)) % modulo_value
43
44
45def solution(base: int = 1777, height: int = 1855, digits: int = 8) -> int:

Callers 1

solutionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected