Consider the algorithm: Algorithm Input: A natural number n (1) let c := n (2) repeat -- (a) let c := c + 4 (3) until c ? 10 (5) return c What does the algorithm return when the input is n = 5 ? What does the algorithm return when the input is n = 4? What does the algorithm return when the input is n = 3?
Added by Albert L.
Close
Your feedback will help us improve your experience
Sri K and 99 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Recall the linear search algorithm: procedure linear search (x: integer, a1, a2, ..., an: distinct integers) i := 1 while (i ≤ n and x 6= ai) i := i + 1 if i ≤ n then location:= i else location:= 0 return location Apply the linear search algorithm to search for the number 3 in the list 1, 5, 3, 9. (a) In this application of the algorithm, what is the integer n? (b) What is the initial value of i? (c) How many iterations of the while loop occur when applied to this list? (d) What is the value of i after the while loop finishes its iterations? (e) What does the algorithm return?
Sri K.
3. Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) = 1Âł + 2Âł + ...... + nÂł Algorithm S(n) //Input: A positive integer n //Output: The sum of the first n cubes if n = 1 return 1 else return S(n - 1) + n * n * n a. What is its basic operation? b. Set up and solve a recurrence relation for the number of times the algorithm's basic operation (multiplication) is executed. c. How does this algorithm compare with the straightforward nonrecursive algorithm for computing this function?
Akash M.
4) Consider the following recursive algorithm Algorithm Q(n) //Input: A positive integer n if n = 1 return 1 else return Q(n - 1) + 2 * n - 1 a. Determine what this algorithm computes. b. Set up a recurrence relation for the number of multiplications made by this algorithm and solve it. c. Set up a recurrence relation for the number of additions/subtractions made by this algorithm and solve it.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD