Basic Algorithms using Ballerina

Ayesh Almeida
Ballerina Swan Lake Tech Blog
2 min readFeb 20, 2022
Photo by Андрей Сизов on unsplash

Implementing algorithms using a programming language is the best way to get an understanding of the dynamics of a particular programming language. In this article we will be implementing few basic algorithms using Ballerina, an integration language to develop cloud-native applications.

N-th Fibonacci Number

In mathematics, the Fibonacci numbers, form a sequence, the Fibonacci sequence, in which each number is the sum of the two preceding ones. The sequence starts from 0 and 1.

eg: 0, 1, 1, 2, 3, 5, 8, 13…

Fibonacci algorithm could also be implemented without recursion.

Prime Number

A number which cannot be divided by any number other than 1 and itself is called a Prime Number.

eg: 2, 3, 5, 7…

N-th Factorial

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Factorial algorithm could also be implemented without recursion.

Palindromic Number

A Palindromic Number (also known as a numeral palindrome or a numeric palindrome) is a number (such as 16461) that remains the same when its digits are reversed.

Binary Palindrome Number

A Binary Palindrome Number is a number whether the binary representation and the reverse of the binary representation is the same.

eg: 5 (binary representatin -> 101 | reverse of binary representation -> 101)

Armstrong Number

In number theory, a Narcissistic Number (also known as a Pluperfect digital invariant, an Armstrong number (after Michael F. Armstrong) or a Plus perfect number) is a number that is the sum of its own digits each raised to the power of the number of digits.

eg: 153 = 1³ + 5³ + 3³

In this article we identified how we could implement few basic algorithms in Ballerina. If you are interested on learning Ballerina Basics, go through my article series or Ballerina Site.

--

--

Ayesh Almeida
Ballerina Swan Lake Tech Blog

A Tech Enthusiast who wants to build simple solutions for complex problems.