Fibonacci series using Recursion in Kotlin
In this tutorial, we will learn how to generate the Fibonacci series using recursion in Kotlin.
Examples
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1.
Example 1: Generating Fibonacci series
In this example, we will create a recursive function that generates the Fibonacci series up to a given number of terms.
Kotlin Program
fun generateFibonacciSeries(terms: Int): List<Int> {
val fibonacciSeries = mutableListOf<Int>()
for (i in 0 until terms) {
fibonacciSeries.add(fibonacci(i))
}
return fibonacciSeries
}
fun fibonacci(n: Int): Int {
return if (n <= 1) {
n
} else {
fibonacci(n - 1) + fibonacci(n - 2)
}
}
fun main() {
val numberOfTerms = 10
val fibonacciSeries = generateFibonacciSeries(numberOfTerms)
println("Fibonacci Series:")
for (term in fibonacciSeries) {
print("$term ")
}
}
Output
Fibonacci Series:
0 1 1 2 3 5 8 13 21 34
The function generateFibonacciSeries
takes an integer terms
as input and returns a list containing the first terms
Fibonacci numbers. It uses a For loop to iterate from 0 to terms
and calls the recursive function fibonacci
to calculate each Fibonacci number.
The fibonacci
function is a recursive function that calculates the nth Fibonacci number. It returns the number itself if n
is less than or equal to 1; otherwise, it calls itself recursively with n - 1
and n - 2
as arguments and returns the sum of the two recursive calls.
Example 2: Generating Fibonacci series up to a specific number
In this example, we will create a recursive function that generates the Fibonacci series up to a specific number.
Kotlin Program
fun generateFibonacciSeriesUpToNumber(number: Int): List<Int> {
val fibonacciSeries = mutableListOf<Int>()
var i = 0
var fibonacciNumber = fibonacci(i)
while (fibonacciNumber <= number) {
fibonacciSeries.add(fibonacciNumber)
i++
fibonacciNumber = fibonacci(i)
}
return fibonacciSeries
}
fun fibonacci(n: Int): Int {
return if (n <= 1) {
n
} else {
fibonacci(n - 1) + fibonacci(n - 2)
}
}
fun main() {
val number = 50
val fibonacciSeries = generateFibonacciSeriesUpToNumber(number)
println("Fibonacci Series up to $number:")
for (term in fibonacciSeries) {
print("$term ")
}
}
Output
Fibonacci Series up to 50:
0 1 1 2 3 5 8 13 21 34
The function generateFibonacciSeriesUpToNumber
takes an integer number
as input and returns a list containing the Fibonacci numbers up to and including number
. It uses a While loop to iterate and add Fibonacci numbers to the list until the next number exceeds the given number
. The calculation of Fibonacci numbers is done using the recursive function fibonacci
as explained in Example 1.
Summary
In this Kotlin Tutorial, we learned how to generate the Fibonacci series using recursion in Kotlin. We saw two examples: one to generate the series up to a given number of terms and another to generate the series up to a specific number.
Note: Recursion is a powerful technique in programming, but it should be used with caution to prevent stack overflow for large values.