2
6.189 IAP 2011: Optional Recursion Exercises These exercises are optional, have fun playing around with them. Solutions will be posted to the website on Tuesday; feel free to ask questions about these problems on the staff email list, or at office hours. For all these problems, be sure to carefully consider your base and recursive cases carefully! 1. Write a function that takes in two numbers and recursively multiplies them together. 2. Write a function that takes in a base and an exp and recursively computes base exp . You are not allowed to use the ** operator! 3. Write a function using recursion to print numbers from n to 0. 4. Write a function using recursion to print numbers from 0 to n (you just need to change one line in the program of problem 1). 5. Write a function using recursion that takes in a string and returns a reversed copy of the string. The only string operation you are allowed to use is string concatenation. 6. Write a function using recursion to check if a number n is prime (you have to check whether n is divisible by any number below n). 7. Write a recursive function that takes in one argument n and computes F n , the n th value of the Fibonacci sequence. Recall that the Fibonacci sequence is defined by the relation F n = F n1 + F n2 where F 0 = 0 and F 1 =1 Visit the Wikipedia page on the Fibonacci Number for more information if you’re still confused.

MIT6 189IAP11 Rec Problems

Embed Size (px)

Citation preview

Page 1: MIT6 189IAP11 Rec Problems

6.189 IAP 2011: Optional Recursion Exercises

These exercises are optional, have fun playing around with them. Solutions will be posted to the website on Tuesday; feel free to ask questions about these problems on the staff email list, or at office hours.

For all these problems, be sure to carefully consider your base and recursive cases carefully!

1. Write a function that takes in two numbers and recursively multiplies them together.

2. Write a function that takes in a base and an exp and recursively computes baseexp. You are not allowed to use the ** operator!

3. Write a function using recursion to print numbers from n to 0.

4. Write a function using recursion to print numbers from 0 to n (you just need to change one line in the program of problem 1).

5. Write a function using recursion that takes in a string and returns a reversed copy of the string. The only string operation you are allowed to use is string concatenation.

6. Write a function using recursion to check if a number n is prime (you have to check whether n is divisible by any number below n).

7. Write a recursive function that takes in one argument n and computes Fn, the nth value of the Fibonacci sequence. Recall that the Fibonacci sequence is defined by the relation

Fn = Fn−1 + Fn−2

whereF0 = 0 and F1 = 1

Visit the Wikipedia page on the Fibonacci Number for more information if you’re still confused.

Page 2: MIT6 189IAP11 Rec Problems

MIT OpenCourseWare http://ocw.mit.edu

6.189 A Gentle Introduction to Programming January IAP 2011

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.