Skip to content

Instantly share code, notes, and snippets.

@sasssass
Created October 19, 2023 11:50
Show Gist options
  • Save sasssass/259082d81329a0463f2c23b9b88e8dc8 to your computer and use it in GitHub Desktop.
Save sasssass/259082d81329a0463f2c23b9b88e8dc8 to your computer and use it in GitHub Desktop.
functional.kt
fun main() {
val numbers = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val sum = sumOfEvenNumbers(numbers, 0, 0)
println("Sum of even numbers: $sum")
}
fun sumOfEvenNumbers(numbers: IntArray, index: Int, currentSum: Int): Int {
return when {
index == numbers.size -> currentSum
numbers[index] % 2 == 0 -> sumOfEvenNumbers(numbers, index + 1, currentSum + numbers[index])
else -> sumOfEvenNumbers(numbers, index + 1, currentSum)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment