scp1: finish task 2
This commit is contained in:
@@ -6,10 +6,14 @@ import scala.annotation.tailrec
|
||||
for (i <- 1 to 50)
|
||||
values.append(i)
|
||||
|
||||
println(values)
|
||||
println(iterative_sum(values.toArray))
|
||||
println(recursive_sum(values.toArray))
|
||||
println((0 to 10).map(fibonacci))
|
||||
println(s"values: $values")
|
||||
println(s"iterative sum of values: ${iterative_sum(values.toArray)}")
|
||||
println(s"recursive sum of values: ${recursive_sum(values.toArray)}")
|
||||
println(s"first fibonacci numbers: ${(0 to 10).map(fibonacci)}")
|
||||
println(s"solutions for 2x² + x - 1: ${quadratic(2, 1, -1)}")
|
||||
println(s"solutions for 2x² + x + 1: ${quadratic(2, 1, 1)}")
|
||||
println(s"solutions for x²: ${quadratic(1, 0, 0)}")
|
||||
println(s"3x² + 2x + 1 evaluated at x = 2 is ${polynomial(3, 2, 1)(2)}")
|
||||
|
||||
// task 1 b)
|
||||
def iterative_sum(numbers: Array[Int]): Int =
|
||||
@@ -29,3 +33,13 @@ def recursive_sum(numbers: Array[Int]): Int =
|
||||
def fibonacci(n: Int): BigInt =
|
||||
if n <= 1 then BigInt(1)
|
||||
else fibonacci(n - 1) + fibonacci(n - 2)
|
||||
|
||||
// task 2 a)
|
||||
def quadratic(a: Double, b: Double, c: Double): Option[(Double, Double)] =
|
||||
val disc = math.pow(b, 2) - 4 * a * c
|
||||
if disc < 0 then None
|
||||
else Some((-b - math.sqrt(disc)) / (2 * a), (-b + math.sqrt(disc)) / (2 * a))
|
||||
|
||||
// task 2 a)
|
||||
def polynomial(a: Double, b: Double, c: Double): Double => Double =
|
||||
(x: Double) => a * x * x + b * x + c
|
||||
|
||||
Reference in New Issue
Block a user