How to resolve the algorithm Modular exponentiation step by step in the Python programming language
How to resolve the algorithm Modular exponentiation step by step in the Python programming language
Table of Contents
Problem Statement
Find the last 40 decimal digits of
a
b
{\displaystyle a^{b}}
, where
A computer is too slow to find the entire value of
a
b
{\displaystyle a^{b}}
. Instead, the program must use a fast algorithm for modular exponentiation:
a
b
mod
m
{\displaystyle a^{b}\mod m}
. The algorithm must work for any integers
a , b , m
{\displaystyle a,b,m}
, where
b ≥ 0
{\displaystyle b\geq 0}
and
m
0
{\displaystyle m>0}
.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Modular exponentiation step by step in the Python programming language
Explanation of the Code:
This Python code calculates the result of raising a large number a
to the power of another large number b
modulo a given modulus m
. It provides both a solution using the built-in pow()
function and a custom implementation without using built-in functions.
Built-In Power Calculation:
pow(a, b, m)
calculates the result ofa
raised to the power ofb
modulom
. It uses the fast exponentiation algorithm to efficiently compute the result while handling the modulo operation.
Custom Power Calculation (Without Built-In Functions):
- The
power_mod
function is a custom implementation of modulo exponentiation. It uses a loop to repeatedly squareb
and update the resultx
based on whethere
(the exponent) is odd or even.
Detailed Breakdown:
-
Line 1:
a
andb
are assigned large integer values.m
is assigned the modulus value (10^40).- The result of
a
raised to the power ofb
modulom
is calculated using thepow()
function and printed.
-
Line 6-14:
- The
power_mod
function is defined to calculate the result of modulo exponentiation.
- The
-
Line 7:
- It initializes
x
to 1.x
will store the intermediate result.
- It initializes
-
Line 8:
- The
while
loop continues untile
becomes 0.
- The
-
Line 9:
b
is squared, ande
is halved. This is the key step in fast exponentiation.
-
Line 10-11:
- If
e
is odd,x
is updated by multiplying it with the currentb
. Otherwise,x
bleibt unverändert.
- If
-
Line 15-18:
power_mod
is called to calculate the result ofa
raised to the power ofb
modulom
. The result is printed.
Source code in the python programming language
a = 2988348162058574136915891421498819466320163312926952423791023078876139
b = 2351399303373464486466122544523690094744975233415544072992656881240319
m = 10 ** 40
print(pow(a, b, m))
def power_mod(b, e, m):
" Without using builtin function "
x = 1
while e > 0:
b, e, x = (
b * b % m,
e // 2,
b * x % m if e % 2 else x
)
return x
a = 2988348162058574136915891421498819466320163312926952423791023078876139
b = 2351399303373464486466122544523690094744975233415544072992656881240319
m = 10 ** 40
print(power_mod(a, b, m))
You may also check:How to resolve the algorithm List rooted trees step by step in the J programming language
You may also check:How to resolve the algorithm Infinity step by step in the APL programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the MAXScript programming language
You may also check:How to resolve the algorithm Abstract type step by step in the REBOL programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the Jsish programming language