Exploring Fibonacci Series in Ballerina: Iterative vs. Recursive Approaches
Fibonacci Series Using Ballerina:
The Fibonacci series is an easy concept in programming, often used to show how recursion works. Each number in the series is the sum of the two numbers before it, starting with 0 and 1. In this blog, we’ll explore how to generate the Fibonacci series using the Ballerina programming language, both iteratively and recursively.
What is Fibonacci Series?:
The Fibonacci series starts with 0 and 1, and each following number is the sum of the previous two numbers. Here’s how the series looks for the first few terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34...
In mathematical terms, the Fibonacci sequence can be defined as:
Fib(0) = 0
Fib(1) = 1 *Fib(n) = Fib(n - 1) + Fib(n - 2) for n > 1
Steps of Fibonacci Series in Iterative Approach:
Start with an array containing the first two terms 0 and 1.
Use a foreach loop to calculate each following term in the series.
For each new term, add the last two numbers in the array to get the next number in the series and append this new term to the array.
Once the loop has generated the desired number of terms, return the entire array containing the complete Fibonacci sequence up to the specified term.
Steps of Fibonacci Series in Recursive Approach:
generateFibonacci function creates the whole Fibonacci series by calling fibonacciRecursive to get each term in the series
Start a loop with i beginning at 0 and continuing up to n - 1, where n is the number of terms we want in the Fibonacci series.
For each value of i, call the fibonacciRecursive(i) function to calculate the i-th Fibonacci number.
Take the result of fibonacciRecursive(i) and add (push) it to the fibSeries array.
The fibonacciRecursive function follows the Fibonacci rule by calling itself with n - 1 and n - 2 until it reaches the base cases of 0 or 1.
Calculate the current term using fibonacciRecursive(n -1) + fibonacciRecursive(n - 2).
Base case reaches if n <= 1.
In the base case simply return n.
Implementation of Fibonacci Series in Ballerina:
Start by creating a new Ballerina project.
Open a terminal and enter the following command:
bal new fibonacci_example
.Open the
main.bal
file and write the Fibonacci Series code.Save the code and run the program using this command in the terminal:
bal run
.
Code Snippet in Iterative Approach:
Following is the coding implementation of the Fibonacci series in an iterative approach:
import ballerina/io;
// Iterative function to generate Fibonacci series
function fibonacciIterative(int n) returns int[] {
int[] fibSeries = [0, 1];
// Generate the series until it reaches 'n' terms
foreach int i in 2 ... n - 1 {
int nextTerm = fibSeries[i - 1] + fibSeries[i - 2];
fibSeries.push(nextTerm);
}
return fibSeries;
}
public function main() {
int n = 10;
io:println("Fibonacci Series in Iterative approach: ", fibonacciIterative(n));
}
Code Snippet in Recursive Approach:
Following is the coding implementation of the Fibonacci series in a recursive approach:
import ballerina/io;
// Recursive function to get the term
function fibonacciRecursive(int n) returns int {
if n <= 1 {
return n;
}
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}
// Recursive function to generate Fibonacci terms up to n
function generateFibonacci(int n) returns int[] {
int[] fibSeries = [];
foreach int i in 0 ... n - 1 {
fibSeries.push(fibonacciRecursive(i));
}
return fibSeries;
}
public function main() {
int n = 10;
io:println("Fibonacci Series in recursive approach: ", generateFibonacci(n));
}
Subscribe to my newsletter
Read articles from Faria Karim directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Faria Karim
Faria Karim
Currently, I am working as an "Associate Software Engineer" at "Kaz Software". I have completed my graduation from North South University in the year 2020 with the highest distinction. I always love to explore new ideas. I am a curious and self-motivated person. So, I always try to keep learning and share my ideas with other people. I enjoy problem-solving a lot. I have solved around 100+ problems in various online judges and I am also a three-star coder at Hackerrank. Moreover, I have participated in the ACM ICPC two times. I have completed the off-sight round of ACM ICPC of 2018. I have some basic knowledge of c/c++, Java, Javascript, Php, and Python. I have done several projects using these programming languages. I have hands-on experience on various backend frameworks like Node.js, Django, Flask, and Laravel. I have also used React.js as the front-end library for a few of my projects. Currently, I am more into React.js. For web-based projects, I have worked on both MySQL and NoSQL (MongoDB) as the database system. Machine learning is my another matter of interest. I have research experience in the fuzzy system. I have done a few projects using Tensorflow, Keras, CNN, and LSTM. Recently I have grown my interest in game development. I have built two games. One is using construct 2 and the another one is in Unity 3D. Side by side, I am also trying to figure out the things on ionic. In my free time, I mostly like to surf the internet, watch movies, and also love to do animations and illustrations using Powerpoint.