Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.annotation.tailrec
- object FactorialExample {
- def main(args: Array[String]): Unit = {
- (1 to 100).foreach(i => println(s"Factorial of ${i} is " + factorial(i)))
- }
- def factorial(i: Int): Int = {
- @tailrec
- def fact(i: Int, acc: Int): Int = {
- if (i <= 0) acc
- else fact(i - 1, acc * i)
- }
- fact(i, 1)
- }
- }
Add Comment
Please, Sign In to add comment