Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please write the following code in Scheme programming language: 3. Binary exponentiat ion Consider the following function, (power base exp), that raises a number (base)
Please write the following code in Scheme programming language:
3. Binary exponentiat ion Consider the following function, (power base exp), that raises a number (base) to a power exp): (define (power base exp) (cond ((= exp 0) 1) (else (* base (power base (- exp 1)))))) (a) There are more efficient means of exponentiation. Design a Scheme function fast exp which calculates be for any e > 0 by the rule: 1 if e=0, be = (62)2 if e is even, I b*be-1 if e is odd. You may find it useful to define square as a separate function for use inside your fast-exp function. (b) Show that the fast exp function is indeed faster than the power function by comparing the number of multiplications that must be done for some exponent e in both functions. (You can assume the exponent eis of the form 2k.) 4. It is an interesting fact the the square root of any number may be expressed as a continued fraction. For example, 1-1 1-1 I=1+ 2+ - -1 2+ (a) In order to do the next part, you will need to write a help er function cont-frac. First, write the recurrence relation for cont-frac, that is, the mathematical expression of the base case and the general case solution for cont-frac. See the rule for be above for an example of a recurrence relation. Hint: It may be clearer to rearrange the above equation as -1 -1= -1 2+ 2 1 - 1 2+ (b) Write a Scheme function called new-sqrt which takes two formal parameters I and n, where r is the number we wish to find the square root of and n is the number of continued fractions to compute recursively. Demonstrate that for large n, new-sqrt is very close to the builtin sort function. Use cont-frac (as you defined above) as a helper function defined within new-sqrt. 3. Binary exponentiat ion Consider the following function, (power base exp), that raises a number (base) to a power exp): (define (power base exp) (cond ((= exp 0) 1) (else (* base (power base (- exp 1)))))) (a) There are more efficient means of exponentiation. Design a Scheme function fast exp which calculates be for any e > 0 by the rule: 1 if e=0, be = (62)2 if e is even, I b*be-1 if e is odd. You may find it useful to define square as a separate function for use inside your fast-exp function. (b) Show that the fast exp function is indeed faster than the power function by comparing the number of multiplications that must be done for some exponent e in both functions. (You can assume the exponent eis of the form 2k.) 4. It is an interesting fact the the square root of any number may be expressed as a continued fraction. For example, 1-1 1-1 I=1+ 2+ - -1 2+ (a) In order to do the next part, you will need to write a help er function cont-frac. First, write the recurrence relation for cont-frac, that is, the mathematical expression of the base case and the general case solution for cont-frac. See the rule for be above for an example of a recurrence relation. Hint: It may be clearer to rearrange the above equation as -1 -1= -1 2+ 2 1 - 1 2+ (b) Write a Scheme function called new-sqrt which takes two formal parameters I and n, where r is the number we wish to find the square root of and n is the number of continued fractions to compute recursively. Demonstrate that for large n, new-sqrt is very close to the builtin sort function. Use cont-frac (as you defined above) as a helper function defined within new-sqrtStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started