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:44] pdmatei |
fp:lab02 [2023/03/10 10:16] (current) pdmatei |
||
---|---|---|---|
Line 12: | Line 12: | ||
<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 45: | Line 45: | ||
**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._''). |