Pascal triangle in scala

To start with any new programming language and for understanding the language fundamentals we will try to execute some Hello world programs and mathematical puzzles to ensure our fundamental understanding of language concepts. I’m just started learning a new programming language Scala, a modern JVM based language with both functional and object oriented paradigms and I have written a program to print pascal triangle using recursion in scala.

scalaIn mathematics, Pascal’s triangle is a triangular array of the binomial coefficients. It is named after the French mathematician Blaise Pascal in much of the Western world

The rows of Pascal’s triangle are conventionally enumerated starting with row n = 0 at the top. The entries in each row are numbered from the left beginning with k = 0 and are usually staggered relative to the numbers in the adjacent rows. A simple construction of the triangle proceeds in the following manner. On row 0, write only the number 1. Then, to construct the elements of following rows, add the number above and to the left with the number above and to the right to find the new value. If either the number to the right or left is not present, substitute a zero in its place. For example, the first number in the first row is 0 + 1 = 1, whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row.

Pascal Triangle


Image credit:

Pascal triangle implementation in scala


Leave a Comment