Convert decimal to binary in Kotlin

Convert decimal number to binary in Kotlin PROBLEM Given a number in the decimal system, convert it to the binary system. From base 10 to base 2. Solution One solution is to keep dividing the given decimal number by 2 and checking its quotient [1]. For example: The code above will return a String representing […]

Calculate Pi (π) using Leibniz formula

Calculate Pi (π) using Leibniz formula problem It is pi day and you wish to write some code in your favorite language to calculate its value. You can pick Leibniz’s formula and implement it in Kotlin. Recall the Leibniz formula: SOLUTION The series above can be implemented as follow (one solution): Pi.kt fun calculatePi(): Double […]

Recursive factorial in Kotlin

Recursive factorial in Kotlin problem Let’s calculate the factorial of a given number n using a recursive approach. Recall the mathematical formula of factorial: n! = n × (n – 1)! SOLUTION fun fact(n:Int):Long = if (n==0) 1 else n * fact(n-1) running To run the above we can call it from the main function. […]