El Noguer

Joaquim Perez i Noguer

Catalan numbers en Scala

Filed under: General — Joaquim Perez Noguer at 6:14 pm on Dissabte, Febrer 9, 2019

Demostració de com implementar la seqüència de números Catalans amb Scala. Vist a Rosetta code.


object Catalan {
  def factorial(n: BigInt) = BigInt(1).to(n).foldLeft(BigInt(1))(_ * _)
  def catalan(n: BigInt) = factorial(2 * n) / (factorial(n + 1) * factorial(n))
 
  def main(args: Array[String]) {
    for (n <- 0 to 15) {
      println("catalan(" + n + ") = " + catalan(n))
    }
  }
}



No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>