How to resolve the algorithm Modular exponentiation step by step in the Python programming language

Published on 12 May 2024 09:40 PM

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 of a raised to the power of b modulo m. 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 square b and update the result x based on whether e (the exponent) is odd or even.

Detailed Breakdown:

  • Line 1:

    • a and b are assigned large integer values.
    • m is assigned the modulus value (10^40).
    • The result of a raised to the power of b modulo m is calculated using the pow() function and printed.
  • Line 6-14:

    • The power_mod function is defined to calculate the result of modulo exponentiation.
  • Line 7:

    • It initializes x to 1. x will store the intermediate result.
  • Line 8:

    • The while loop continues until e becomes 0.
  • Line 9:

    • b is squared, and e 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 current b. Otherwise, x bleibt unverändert.
  • Line 15-18:

    • power_mod is called to calculate the result of a raised to the power of b modulo m. 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