Make task 1 easier to run

This commit is contained in:
Oystein Kristoffer Tveit 2022-10-17 18:37:18 +02:00
parent 74a607d1a6
commit 94bb828dad
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
1 changed files with 33 additions and 24 deletions

View File

@ -1,29 +1,38 @@
object Task1A extends App {
object Main extends App {
//task a
val x = (1 to 50).toList val x = (1 to 50).toList
println(x)
//task b println(x)
}
object Task1B extends App {
def find_sum_of_list(list: List[Int]): Int = { def find_sum_of_list(list: List[Int]): Int = {
var sum: Int = 0 var sum: Int = 0
list.foreach (sum += _) list.foreach (sum += _)
sum sum
} }
println(find_sum_of_list(List(1, 2, 3, 4)))
println(find_sum_of_list(List(1, 2, 3, 4)))
}
object Task1C extends App {
//task c //task c
def find_sum_of_list_rec(list: List[Int]): Int = list match{ def find_sum_of_list_rec(list: List[Int]): Int = list match{
case List() => 0 case List() => 0
case _ => list.head + find_sum_of_list_rec(list.tail) case _ => list.head + find_sum_of_list_rec(list.tail)
} }
println(find_sum_of_list_rec(List(1, 2, 3, 4))) println(find_sum_of_list_rec(List(1, 2, 3, 4)))
}
//task d
def nth_fibonacci(n: BigInt): BigInt = n match object Task1D extends App {
case 0 => 0 val bigZero = BigInt(0)
case 1 => 1 val bigOne = BigInt(1) // 🍕
case _ => nth_fibonacci(n-1) + nth_fibonacci(n-2)
def nth_fibonacci(n: BigInt): BigInt = n match {
println(nth_fibonacci(10)) case `bigZero` => n
case `bigOne` => n
case _ => nth_fibonacci(n-1) + nth_fibonacci(n-2)
}
println(nth_fibonacci(30))
} }