Question
Foundations of algorithm chp 11 #15
15. write an iterative version of Algorithm 11.1.
15. write an iterative version of Algorithm 11.1.
15. write an iterative version of Algorithm 11.1.
15. write an iterative version of Algorithm 11.1.
15. write an iterative version of Algorithm 11.1.
15. write an iterative version of Algorithm 11.1.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The iterative version for given algorithm goes like:

Code:

int gcd ( int n, int m) // assuming n > m else swap those

{

int rem;

while(m!=0)

{

rem = n%m;

n = m;

m = rem;

}

return n;

Explanation:

In this algorithm, the goal is to find the GCD but without using recursion. So, the technique that can be used is as below:

The method to understand it in both recursive and iterative I am considering same as working is same, just the code is different.

Say for instance one needs GCD of 300 and 21. So, the way to find it is, dividing greater number every time so that it shrinks and savingi the remainder. Say for instance, if 21 is not GCD, it will be obiously less than 21. So, to find it out, again division is done but with respect to the remainder. So, GCD(300,21) turns into GCD(21,6). Again, if 21 mod 6 is 0, 6 is GCD, but it is not. The remainder is 3. So, again, in the same way as previous, GCD of 6 and 21%6=3 is found which is 3. so, in turn the GCD is 3.

So, when remainder becomes 0, the other number is given as GCD.

Add a comment
Know the answer?
Add Answer to:
Foundations of algorithm chp 11 #15 15. write an iterative version of Algorithm 11.1. Your algorithm...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 07-15 pts) Develop a recursive version of the Bubble Sort algorithm. (a) Write the pseudo code...

    07-15 pts) Develop a recursive version of the Bubble Sort algorithm. (a) Write the pseudo code of the algorithm and justify that it is recursive and works correctly Write the recurrence relation for the algorithm and solve it using one of the two approaches discussed in class, as appropriate. Solve the recurrence relation and show that the time complexity of the recursive algorithm is θ(n).

  • Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to...

    Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to implement a binary search algorithm to determine whether a given element is a member of a given sequence Each function will have two parameters, aseq, a sorted sequence, and target. isMemberR and isMemberI will return True if target is an element of the sequence, and False otherwise. Your implementations must implement the binary search algorithm described above. When function i sMemberR recursively invokes itself,...

  • 1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci...

    1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci number. By definition Fibonnaci(0) is 1, Fibonnaci(1) is 1, Fibonnaci(2) is 2, Fibonnaci(3) is 3, Fibonnaci(4) is 5, and so on. Your function may only use a constant amount of memory (i.e. no auxiliary array). Argue that the running time of the function is Θ(n), i.e. the function is linear in n. 2. (10 points) Order the following functions by growth rate: N, \N,...

  • Appreciate a step by step explanation to this question. Write two separate programs in python that...

    Appreciate a step by step explanation to this question. Write two separate programs in python that generate the n-th Fibonacci number. The programs should both take n as command line input. The first algorithm one should implement the recursive algorithm, and the second one should implement the iterative algorithm. Measure the time it takes for each of these versions (use for example, the time it module in python) to calculate the n-th Fibonacci number for the following values of n:...

  • (a) Your company participates in a competition and the fastest algorithm wins. You know of two...

    (a) Your company participates in a competition and the fastest algorithm wins. You know of two different algorithms that can solve the problem in the competition. • Algorithm I solves problems by dividing them into five subproblems of half the size, recursively solving each subproblem, and then combining the solutions in linear time. • Algorithm 2 solves problems of size n by dividing them into 16 subproblems of size n/4, recursively solving each subproblem, and then combining the solutions in...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • Write a function in Matlab to compute e^x. Your function should implement a truncated version of...

    Write a function in Matlab to compute e^x. Your function should implement a truncated version of the innite sum e^x = lim_n rightarrow infinity sigma_k=0^n x^k/k!. Compare your function with the built-in function exp(x) for x = plusminus1, plusminus 10, plusminus 15. Specifically, for each x, how many terms n do you need to achieve a relative error smaller than 10^-8? Are the results for x > 0 different from those for x < 0? If so, what is a...

  • Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for...

    Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for “travel arrangement”. For example, at a high level of abstraction, the algorithm for “travel arrangement” is: book a hotel buy a plane ticket rent a car Using the principle of stepwise refinement, write more detailed pseudocode for each of these three steps at a lower level of abstraction. Exercise 2 Asymptotic Complexity (3 pts) Determine the Big-O notation for the following growth functions: 1....

  • Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a...

    Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a high level language of your choice and in Assembly to find a Fibonacci number and check your result. At the end of this assignment a possible solution to this problem is given both in Python and MIPS assembler. Note that it would have been possible to write the Python differently to achieve the same function. . [20 points] Write and debug a MIPS program...

  • PYTHON I need help with this Python problem, please follow all the rules! Please help! THANK...

    PYTHON I need help with this Python problem, please follow all the rules! Please help! THANK YOU! Bonus Question Implement an efficient algorithm (Python code) for finding the 11th largest element in a list of size n. Assume that n will be greater than 11. det find 11th largest numberlissy Given, lissy, a list of n integers, the above function will return the 11th largest integer from lissy You can assume that length of lissy will be greater than 11....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT