Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pp:2023:scala:l01 [2023/03/09 17:18] andrei.cirpici |
pp:2023:scala:l01 [2023/03/15 18:51] (current) pdmatei |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Lab 2. Introduction to Scala ====== | + | ====== Lab 1. Introduction to Scala ====== |
** Objectives: ** | ** Objectives: ** | ||
Line 8: | Line 8: | ||
** Create a new Scala worksheet to write your solutions ** | ** Create a new Scala worksheet to write your solutions ** | ||
- | ===== 2.1. Recursion ===== | + | ===== 1.1. Recursion ===== |
- | **2.1.1.** Write a tail-recursive function that computes the factorial of a natural number. Start from the code stub below: | + | **1.1.1.** Write a tail-recursive function that computes the factorial of a natural number. Start from the code stub below: |
<code scala> | <code scala> | ||
Line 21: | Line 21: | ||
</code> | </code> | ||
- | **2.1.2.** Implement a tail-recursive function that computes the greatest common divisor of two natural number: | + | **1.1.2.** Implement a tail-recursive function that computes the greatest common divisor of two natural number: |
<code scala> | <code scala> | ||
Line 27: | Line 27: | ||
</code> | </code> | ||
- | **2.1.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). | + | **1.1.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 33: | Line 33: | ||
</code> | </code> | ||
- | **2.1.4.** Write a function which computes the sum of all natural numbers within a range. Use **two styles** to write this function: direct recursion, and tail recursion. | + | **1.1.4.** Write a function which computes the sum of all natural numbers within a range. Use **two styles** to write this function: direct recursion, and tail recursion. |
<code scala> | <code scala> | ||
def sumNats(start: Int, stop: Int): Int = ??? | def sumNats(start: Int, stop: Int): Int = ??? | ||
Line 39: | Line 39: | ||
</code> | </code> | ||
- | **2.1.5.** Write a function which computes the sum of all prime numbers within a range. | + | **1.1.5.** Write a function which computes the sum of all prime numbers within a range. |
<code scala> | <code scala> | ||
def sumPrimes(start: Int, stop: Int): Int = ??? | def sumPrimes(start: Int, stop: Int): Int = ??? | ||
</code> | </code> | ||
- | **2.1.6.** (!) Write a function which takes an initial value $math[x] and a range of values $math[x_0, x_1, \ldots, x_n] and computes $math[x - x_0 - x_1 - \ldots x_n]. Use the most appropriate type of recursion for this task. | + | **1.1.6.** (!) Write a function which takes an initial value $math[x] and a range of values $math[x_0, x_1, \ldots, x_n] and computes $math[(\ldots((x - x_0) - x_1) - \ldots x_n)]. Use the most appropriate type of recursion for this task. |
<code scala> | <code scala> | ||
Line 50: | Line 50: | ||
</code> | </code> | ||
- | **2.1.7.** (!) Write a function which takes an initial value $math[x] and a range of values $math[x_0, x_1, \ldots, x_n] and computes $math[x_0 - x_1 - \ldots - x_n - x]. Use the most appropriate type of recursion for this task. | + | **1.1.7.** (!) Write a function which takes an initial value $math[x] and a range of values $math[x_0, x_1, \ldots, x_n] and computes $math[x_0 - (x_1 - \ldots - (x_n - x)\ldots)]. Use the most appropriate type of recursion for this task. |
- | ===== 2.2. Newton's Square Root method ===== | + | ===== 1.2. Newton's Square Root method ===== |
A very fast way to numerically compute $math[\sqrt{a}], often used as a standard //sqrt(.)// implementation, relies on Newton's Square Root approximation. The main idea relies on starting with an estimate (often 1), and incrementally improving the estimate. More precisely: | A very fast way to numerically compute $math[\sqrt{a}], often used as a standard //sqrt(.)// implementation, relies on Newton's Square Root approximation. The main idea relies on starting with an estimate (often 1), and incrementally improving the estimate. More precisely: | ||
Line 59: | Line 59: | ||
* 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.2.1.** Implement the function ''improve'' which takes an estimate $math[x_n] of $math[\sqrt{a}] and improves it (computes $math[x_{n+1}]). | + | **1.2.1.** 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.2.2.** Implement the function ''nthGuess'' which starts with $math[x_0 = 1] and computes the nth estimate $math[x_n] of $math[\sqrt{a}]: | + | **1.2.2.** 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: Int, a: Double): Double = ??? | def nth_guess(n: Int, a: Double): Double = ??? | ||
Line 72: | Line 72: | ||
* 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.2.3.** 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._''). | + | **1.2.3.** 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.2.4.** Implement the function ''mySqrt'' which computes the square root of an integer ''a''. Modify the previous implementations to fit the following code structure: | + | **1.2.4.** 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 89: | Line 89: | ||
</code> | </code> | ||
- | **2.2.5. (!) ** 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). | + | **1.2.5. (!) ** 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). |