Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The following two questions must be answered in the language OCAML and can ONLY USE RECURSION . How would I do this without while loops?
The following two questions must be answered in the language OCAML and can ONLY USE RECURSION. How would I do this without while loops?
In addition, we are provided with the following in our OCAML prelude, to be used in the solution:
exception NotImplemented;;
let close ((x: float), (y: float)) = abs_float (x -. y)
let square (x: float) = x *. x;;
let cube (x:float) = x *. x*. x;;
let odd n = (n mod 2) = 1;;
Q1. [30 pointsl Here is a way to compute the square root of a positive real number say x. We make a wild guess that the square root is some number g. Then we check if g2 is close to z. If it is we return g as our answer; if not we refine our guess using the formula where g' is the new guess. We keep repeating until we are close enough. Write an OCaml program to implement this idea. Since we are working with floating point numbers we cannot check for equality; as well as a function called square. In OCaml floating point operations need special symbols: you need to put a dot after the operation. Thus you need to write, for example, 2.0+-3.0. All the basic arithmetic operations have dotted versions: +., *.,/. and must be used with floating point numbers. You cannot write expressions like 2 +.3.5 or 2.0 +3.0. We will not test your program on negative inputs so we are not requiring you to deal with the possibility that you may get a negative answer. There are of course built-in functions to compute square roots. We have written the tester program to check if you used this; you will get a zero if you use a built-in function. Q2. [30 points] This is similar to the question above except that we are computing cube roots. The formula to refine the guess is where I am using mathematical notation with arithmetic operation symbols overloaded. In your code you must use the floating-point versions of the arithmetic operations Q1. [30 pointsl Here is a way to compute the square root of a positive real number say x. We make a wild guess that the square root is some number g. Then we check if g2 is close to z. If it is we return g as our answer; if not we refine our guess using the formula where g' is the new guess. We keep repeating until we are close enough. Write an OCaml program to implement this idea. Since we are working with floating point numbers we cannot check for equality; as well as a function called square. In OCaml floating point operations need special symbols: you need to put a dot after the operation. Thus you need to write, for example, 2.0+-3.0. All the basic arithmetic operations have dotted versions: +., *.,/. and must be used with floating point numbers. You cannot write expressions like 2 +.3.5 or 2.0 +3.0. We will not test your program on negative inputs so we are not requiring you to deal with the possibility that you may get a negative answer. There are of course built-in functions to compute square roots. We have written the tester program to check if you used this; you will get a zero if you use a built-in function. Q2. [30 points] This is similar to the question above except that we are computing cube roots. The formula to refine the guess is where I am using mathematical notation with arithmetic operation symbols overloaded. In your code you must use the floating-point versions of the arithmetic operationsStep 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