Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
fp:lab02 [2022/02/24 11:31] pdmatei |
fp:lab02 [2023/03/10 10:16] (current) pdmatei |
||
---|---|---|---|
Line 9: | Line 9: | ||
- | 2.1. Write a tail-recursive function that computes the factorial of a natural number. Start from the code stub below: | + | **2.1.** Write a tail-recursive function that computes the factorial of a natural number. Start from the code stub below: |
<code scala> | <code scala> | ||
- | def fact (n: Integer): Integer = { | + | def fact (n: Int): Int = { |
- | def aux_fact(n: Integer, acc: Integer): Integer = | + | def aux_fact(i: Int, acc: Int): Int = |
if (???) acc | if (???) acc | ||
else ??? | else ??? | ||
Line 20: | Line 20: | ||
</code> | </code> | ||
- | 2.2. Implement a tail-recursive function that computes the greatest common divisor of a natural number: | + | **2.2.** Implement a tail-recursive function that computes the greatest common divisor of a natural number: |
<code scala> | <code scala> | ||
Line 26: | Line 26: | ||
</code> | </code> | ||
- | 2.3. Write a tail-recursive function takes an integer $math[n] and computes the value $math[1 + 2^2 + 3^2 + ... + (n-1)^2 + n^2]. (Hint: use inner functions). | + | **2.3.** Write a tail-recursive function takes an integer $math[n] and computes the value $math[1 + 2^2 + 3^2 + ... + (n-1)^2 + n^2]. (Hint: use inner functions). |
<code scala> | <code scala> | ||
Line 38: | Line 38: | ||
* Compute $math[x_{n+1} = \displaystyle\frac{1}{2}(x_n+\frac{a}{x_n})] | * Compute $math[x_{n+1} = \displaystyle\frac{1}{2}(x_n+\frac{a}{x_n})] | ||
- | 2.4. Implement the function ''improve'' which takes an estimate $math[x_n] of $math[\sqrt{a}] and improves it (computes $math[x_{n+1}]). | + | **2.4.** Implement the function ''improve'' which takes an estimate $math[x_n] of $math[\sqrt{a}] and improves it (computes $math[x_{n+1}]). |
<code scala> | <code scala> | ||
def improve(xn: Double, a: Double): Double = ??? | def improve(xn: Double, a: Double): Double = ??? | ||
</code> | </code> | ||
- | 2.5. Implement the function ''nthGuess'' which starts with $math[x_0 = 1] and computes the nth estimate $math[x_n] of $math[\sqrt{a}]: | + | **2.5.** Implement the function ''nthGuess'' which starts with $math[x_0 = 1] and computes the nth estimate $math[x_n] of $math[\sqrt{a}]: |
<code scala> | <code scala> | ||
- | def nth_guess(n: Double, a: Double): Double = ??? | + | def nth_guess(n: Int, a: Double): Double = ??? |
</code> | </code> | ||
Note that: | Note that: | ||
- | * for smaller $math[a], there is no need to compute $math[n] estimations (as $math[(x_n)_n] converges quite fast to $math[\sqrt{a}]. | + | * for smaller $math[a], there is no need to compute $math[n] estimations as $math[(x_n)_n] converges quite fast to $math[\sqrt{a}]. |
- | 2.6. Thus, implement the function ''acceptable'' which returns ''true'' iff $math[\mid x_n^2 - a \mid \leq 0.001]. (Hint, google the ''abs'' function in Scala. Don't forget to import ''scala.math._''). | + | **2.6.** Thus, implement the function ''acceptable'' which returns ''true'' iff $math[\mid x_n^2 - a \mid \leq 0.001]. (Hint, google the ''abs'' function in Scala. Don't forget to import ''scala.math._''). |
<code scala> | <code scala> | ||
def acceptable(xn: Double, a: Double): Boolean = ??? | def acceptable(xn: Double, a: Double): Boolean = ??? | ||
</code> | </code> | ||
- | 2.7. Implement the function ''mySqrt'' which computes the square root of an integer ''a''. Modify the previous implementations to fit the following code structure: | + | **2.7.** Implement the function ''mySqrt'' which computes the square root of an integer ''a''. Modify the previous implementations to fit the following code structure: |
<code scala> | <code scala> | ||
def mySqrt(a: Double): Double = { | def mySqrt(a: Double): Double = { | ||
Line 67: | Line 67: | ||
} | } | ||
</code> | </code> | ||
+ | |||
+ | **2.8. (!) ** Try out your code for: ''2.0e50'' (which is $math[2.0\cdot 10^{50}]) or ''2.0e-50''. The code will likely take a very long time to finish. The reason is that $math[xn^2 - a] will suffer from rounding error which may be larger than 0.001. Can you find a different implementation for the function ''acceptable'' which takes that into account? (Hint: the code is just as simple as the original one). | ||
+ | |||