Guest User

Untitled

a guest
Apr 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. import scala.annotation.tailrec
  2.  
  3. object FactorialExample {
  4.  
  5. def main(args: Array[String]): Unit = {
  6. (1 to 100).foreach(i => println(s"Factorial of ${i} is " + factorial(i)))
  7. }
  8.  
  9. def factorial(i: Int): Int = {
  10. @tailrec
  11. def fact(i: Int, acc: Int): Int = {
  12. if (i <= 0) acc
  13. else fact(i - 1, acc * i)
  14. }
  15.  
  16. fact(i, 1)
  17. }
  18.  
  19. }
Add Comment
Please, Sign In to add comment