From f0ab9867bfa6cd9272ebacb3a314ca5d0145fc79 Mon Sep 17 00:00:00 2001 From: peet Date: Sun, 5 Nov 2023 21:06:09 +0100 Subject: [PATCH] aufgabe 5 angefangen aber noch fehlerhaft bei triangles(sum()) --- src/main/scala/main.scala | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/scala/main.scala b/src/main/scala/main.scala index c6288f2..70ef690 100644 --- a/src/main/scala/main.scala +++ b/src/main/scala/main.scala @@ -59,3 +59,27 @@ def binKoeffRek(m:Int,n:Int):Int = { else return m * binKoeffRek(m - 1, n - 1) / n } + +//Aufgabe 5 + +def triagnles(n:Int):Int = { + + // mistake is in sum it always returns 1 + + def sum(i: Int, n: Int): Int = { + if (i <= n) then{ + // println("Test1") + return (n-i)+sum(i+2,n) + } + else { + 1 + } + } + // println("Test2") + if (n == 1) { + return 1 + } else { + //return n + return triagnles(n - 1) + ((n * (n + 1)) / 2) + sum(1,n) + } +}