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. […]

Recursive factorial in Java

Recursive factorial in Java problem Calculate the factorial of a given number using a recursive approach. Recall the mathematical formula: n! = n × (n – 1)! SOLUTION Let’s implement the formula above in a method as follows: public long factorial(int number) { if (number == 0) { return 1; } else { return number […]

Recursive factorial in Python

Recursive factorial in Python problem Recursion is a type of repetition. In this post, we calculate factorial using recursion. Factorial is a mathematical term given with the following formula: n! = n × (n – 1)! SOLUTION Due to the recursive nature of the formula, it is easier to implement it using recursion as follows: […]