We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Functional Programming
  3. Recursion
  4. Pascal's Triangle
  5. Discussions

Pascal's Triangle

Problem
Submissions
Leaderboard
Discussions

    You are viewing a single comment's thread. Return to all comments →

  • patrick_ce 2 years ago+ 0 comments

    I found easier and cleaner solution. Probably also faster thanks to my "if".

    def main(args: Array[String]) {
     val N = scala.io.StdIn.readInt()
     def factorial(num: Int) = (1 to num).product
     for (n <- 0 to N-1) {
      for (r <- 0 to N) {
       if (r <= n) {
        val x = factorial(n) / (factorial(r) * factorial(n - r))
        print(s"$x ")
       } 
      }
      print("\n")
     }
    }
    

    However still it is far from being perfect due to repeted computation of fatorials... there must be some way how to reuse computations from previous levels. I will try to find it on another day.

    0|
    ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature