Question

Given the three numbers, a, n and m, compute a^n mod m. What is the input...

Given the three numbers, a, n and m, compute a^n mod m. What is the input size and why? What is the brute-force solution for this problem and what is its complexity? Give the most efficient algorithm for this problem that you can, analyze its time complexity T(n), express it as a function of the input size and argue whether or not it is a polynomial-time algorithm.

Homework Answers

Answer #1

Brute-force solution
compute(a,n,m):
   product = 1;
   for i=1 to n:
   product = product * a;
product = product % m;
   print(product);

Time complexity = O(n) Loop from 1 to n

Efficient solution
compute(a,n,m):
   product = 1;
   a = a % m;
   while (n > 0)
   {
// If b is odd, multiply a with product
if (n is odd)
product = (product*a) % m;
  
n = n/2 ;
a = (a*a) % m;   
   }
   print(product);

Time complexity T(n)= O(log n)

In Each iteration of while Loop value of n getting half.
Therefore wile loop runs for 2^k = n
k = log n

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the following function : F 1 = 2, F n = (F n-1 ) 2...
Consider the following function : F 1 = 2, F n = (F n-1 ) 2 , n ≥ 2 i. What is the complexity of the algorithm that computes F n using the recursive definition given above. ii. Describe a more efficient algorithm to calculate F n and give its running time.
Given a collection of n nuts, and a collection of n bolts, each arranged in an...
Given a collection of n nuts, and a collection of n bolts, each arranged in an increasing order of size, give an O(n) time algorithm to check if there is a nut and a bolt that have the same size. You can assume that the sizes of the nuts and bolts are stored in the arraysNUTS[1..n] and BOLTS[1..n], respectively, where NUTS[1] < ··· < NUTS[n] and BOLTS[1] < ··· < BOLTS[n]. Note that you only need to report whether or...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
In this problem your task is to find a missing number. The input will always consist...
In this problem your task is to find a missing number. The input will always consist of an array of n positive integers such that the difference between every two consecutive numbers is a fixed constant but one integer is missing. See below for two example inputs/outputs: Input sequence: [0, 2, 4, 6, 10] Output: missing number is 8 Input sequence: [1, 4, 7, 13, 16] Output: missing number is 10 Note that in the first example the constant c...
What does the following function compute? Give an analysis of its complexity int fun1 (int n)...
What does the following function compute? Give an analysis of its complexity int fun1 (int n) { if (n == 0)     return 1; else      return fun1(n-1) + fun1(n-1); }
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 2. Consider the following algorithm for finding the distance between the two closest elements in an array of numbers. ALGORITHM MinDistance(A[0..n − 1])//Input: Array A[0..n − 1] of numbers //Output: Minimum distance between two of its...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 1.Design an algorithm to find all the common elements in two sorted lists of numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2,...
You're are working on n different projects, but in m hours you are going on vacation....
You're are working on n different projects, but in m hours you are going on vacation. Imagine that for each project i, you had a function fi that told you how happy the people paying for project i would be if out your m available hours you devoted 0 ≤ x ≤ m to project i. Imagine further that each such function has maximum value at most s, corresponding to the project being fully finished (and thus the clients being...
1.Let f and g be two functions such that f(n)/g(n) converges to a positive value less...
1.Let f and g be two functions such that f(n)/g(n) converges to a positive value less than 1 as n tends to infinity. Which of the following is necessarily true? Select one: a. g(n)=Ω(f(n)) b. f(n)=Ω(g(n)) c. f(n)=O(g(n)) d. g(n)=O(f(n)) e. All of the answers 2. If T(n)=n+23 log(2n) where the base of the log is 2, then which of the following is true: Select one: a. T(n)=θ(n^2) b. T(n)=θ(n) c. T(n)=θ(n^3) d. T(n)=θ(3^n) 3. Let f and g be...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT