Question

In: Computer Science

Use Recursive Algorithm to compute 5^23 Mod 8

Use Recursive Algorithm to compute 5^23 Mod 8

Solutions

Expert Solution

Answer :

5

Explanation :

Given three numbers a, b and c, we need to find (ab) % c

if b is even:
(a ^ b) % c = ((a ^ b/2) * (a ^ b/2))%c ? this suggests divide and conquer
if b is odd:
(a ^ b) % c = (a * (a ^( b-1))%c

Here the C++ program

// Recursive C++ program to compute modular power 
#include <bits/stdc++.h> 
using namespace std; 

int exponentMod(int A, int B, int C) 
{ 
        // Base cases 
        if (A == 0) 
                return 0; 
        if (B == 0) 
                return 1; 

        // If B is even 
        long y; 
        if (B % 2 == 0) { 
                y = exponentMod(A, B / 2, C); 
                y = (y * y) % C; 
        } 

        // If B is odd 
        else { 
                y = A % C; 
                y = (y * exponentMod(A, B - 1, C) % C) % C; 
        } 

        return (int)((y + C) % C); 
} 

// Driver code 
int main() 
{ 
        int A = 5, B = 23, C = 8; 
        cout << "Power is " << exponentMod(A, B, C); 
        return 0; 
} 

// This code is contributed by SHUBHAMSINGH10 

Related Solutions

Use the Extended Euclid's Algorithm to solve ƒ-1  for 8 mod 11
Use the Extended Euclid's Algorithm to solve ƒ-1  for 8 mod 11
1. Use the Extended Euclid's Algorithm to solve ƒ-1 for 8 mod 11 2. Use the...
1. Use the Extended Euclid's Algorithm to solve ƒ-1 for 8 mod 11 2. Use the max function to calculate max3(x, y, z) if x = 2, y = 6, z = 5. Show your work!
a) Use Fermat’s little theorem to compute 52003 mod 7, 52003 mod 11, and 52003 mod 13.
  a) Use Fermat’s little theorem to compute 52003 mod 7,52003 mod 11, and 52003 mod 13. b) Use your results from part (a) and the Chinese remaindertheorem to find 52003 mod 1001. (Note that1001 = 7 ⋅ 11 ⋅ 13.)
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
Give a recursive algorithm to compute a list of all permutations of a given set S....
Give a recursive algorithm to compute a list of all permutations of a given set S. (That is, compute a list of all possible orderings of the elements of S. For example, permutations({1, 2, 3}) should return {〈1, 2, 3〉, 〈1, 3, 2〉, 〈2, 1, 3〉, 〈2, 3, 1〉, 〈3, 1, 2〉, 〈3, 2, 1〉}, in some order.) Prove your algorithm correct by induction.
Compute the following: (a) 13^2018 (mod 12) (b) 8^11111 (mod 9) (c) 7^256 (mod 11) (d)...
Compute the following: (a) 13^2018 (mod 12) (b) 8^11111 (mod 9) (c) 7^256 (mod 11) (d) 3^160 (mod 23)
2. Give a recursive algorithm to compute the product of two positive integers, m and n,...
2. Give a recursive algorithm to compute the product of two positive integers, m and n, using only addition and subtraction. Java Language...
use algorithm modular exponentiation to find 11^644 mod 645
use algorithm modular exponentiation to find 11^644 mod 645
1. Use backward substitution to solve: x=8 (mod 11) x=3 (mod 19)
  1. Use backward substitution to solve: x=8 (mod 11) x=3 (mod 19) 2. Fine the subgroup of Z24 (the operation is addition) generates by the element 20. 3. Find the order of the element 5 in (z/7z)
5. (20 marks) Write a recursive Bubble Sort algorithm that takes an array A of n...
5. Write a recursive Bubble Sort algorithm that takes an array A of n numbers as input. Analyze its time complexity using a recursion tree. Implement your algorithm in Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT