Solution: Range Operations

This is the expected solution:

fun calculateSumOfSquares(n: Int): Int { var sum = 0 for (i in 1..n) { sum += i * i } return sum } fun calculateSumOfEven(n: Int): Int { var sum = 0 for (i in 2..n step 2) { sum += i } return sum } fun countDownByStep(start: Int, end: Int, step: Int): String { var result = "" for (i in start downTo end step step) { result += i if (i != end) { result += ", " } } return result }

This exercise can also be solved using functions that are presented in the Functional Kotlin book (the next book I recommend you reading after Kotlin Essentials). This is a solution using fold function:

fun calculateSumOfSquares(n: Int): Int = (1..n).fold(0) { sum, i -> sum + i * i } fun calculateSumOfEven(n: Int): Int = (2..n step 2).fold(0) { sum, i -> sum + i } fun countDownByStep(start: Int, end: Int, step: Int): String = (start downTo end step step).fold("") { result, i -> result + i + if (i != end) ", " else "" }

This is a solution using sumOf, sum and joinToString functions:

fun calculateSumOfSquares(n: Int): Int = (1..n).sumOf { it * it } fun calculateSumOfEven(n: Int): Int = (0..n step 2).sum() fun countDownByStep(start: Int, end: Int, step: Int): String = (start downTo end step step).joinToString(separator = ", ")

Example solution in playground

import org.junit.Test import kotlin.test.assertEquals fun calculateSumOfSquares(n: Int): Int { var sum = 0 for (i in 1..n) { sum += i * i } return sum } fun calculateSumOfEven(n: Int): Int { var sum = 0 for (i in 2..n step 2) { sum += i } return sum } fun countDownByStep(start: Int, end: Int, step: Int): String { var result = "" for (i in start downTo end step step) { result += i if (i != end) { result += ", " } } return result } fun main() { // Examples for calculateSumOfSquares println(calculateSumOfSquares(0)) // 0 println(calculateSumOfSquares(1)) // 1 println(calculateSumOfSquares(2)) // 5 (1 + 4) println(calculateSumOfSquares(3)) // 14 (1 + 4 + 9) println(calculateSumOfSquares(4)) // 30 (1 + 4 + 9 + 16) // Example for calculateSumOfEven println(calculateSumOfEven(0)) // 0 println(calculateSumOfEven(1)) // 0 println(calculateSumOfEven(2)) // 2 println(calculateSumOfEven(3)) // 2 println(calculateSumOfEven(5)) // 6 (2 + 4) println(calculateSumOfEven(10)) // 30 (2 + 4 + 6 + 8 + 10) println(calculateSumOfEven(12)) // 42 (2 + 4 + 6 + 8 + 10 + 12) println(calculateSumOfEven(20)) // 110 (2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20) // Example for countDownByStep println(countDownByStep(1, 1, 1)) // 1 println(countDownByStep(5, 1, 2)) // 5, 3, 1 println(countDownByStep(10, 1, 3)) // 10, 7, 4, 1 println(countDownByStep(15, 5, 5)) // 15, 10, 5 println(countDownByStep(20, 2, 3)) // 20, 17, 14, 11, 8, 5, 2 println(countDownByStep(10, 4, 3)) // 10, 7, 4 println(countDownByStep(-1, -1, 1)) // -1 println(countDownByStep(-5, -9, 2)) // -5, -7, -9 } class LoopsTest { @Test fun testCalculateSumOfSquares() { assertEquals(1, calculateSumOfSquares(1)) assertEquals(5, calculateSumOfSquares(2)) assertEquals(14, calculateSumOfSquares(3)) assertEquals(30, calculateSumOfSquares(4)) assertEquals(385, calculateSumOfSquares(10)) assertEquals(0, calculateSumOfSquares(0)) assertEquals(0, calculateSumOfSquares(-1)) assertEquals(0, calculateSumOfSquares(-1)) assertEquals(0, calculateSumOfSquares(-3)) } @Test fun testCalculateSumOfEven() { assertEquals(0, calculateSumOfEven(0)) assertEquals(0, calculateSumOfEven(1)) assertEquals(2, calculateSumOfEven(2)) assertEquals(2, calculateSumOfEven(3)) assertEquals(6, calculateSumOfEven(5)) assertEquals(30, calculateSumOfEven(10)) assertEquals(42, calculateSumOfEven(12)) assertEquals(110, calculateSumOfEven(20)) assertEquals(240, calculateSumOfEven(30)) assertEquals(0, calculateSumOfEven(-1)) } @Test fun testCountDownByStep() { assertEquals("1", countDownByStep(1, 1, 1)) assertEquals("5, 3, 1", countDownByStep(5, 1, 2)) assertEquals("10, 7, 4, 1", countDownByStep(10, 1, 3)) assertEquals("15, 10, 5", countDownByStep(15, 5, 5)) assertEquals("20, 17, 14, 11, 8, 5, 2", countDownByStep(20, 2, 3)) assertEquals("10, 7, 4", countDownByStep(10, 4, 3)) assertEquals("-1", countDownByStep(-1, -1, 1)) assertEquals("-5, -7, -9", countDownByStep(-5, -9, 2)) assertEquals("0", countDownByStep(0, 0, 1)) assertEquals("0", countDownByStep(0, 0, 2)) } }