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.

In 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.

[Source: http://en.wikipedia.org/wiki/Pascal%27s_triangle]

Image credit:http://en.wikipedia.org/wiki/File:PascalTriangleAnimated2.gif

## Pascal triangle implementation in scala

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package org.j2eedev.algorithmicpuzzles.scala object Main { /** * https://j2eedev.org */ def main(args: Array[String]) { println("Pascal's Triangle") for (row <- 0 to 10) { for (col <- 0 to row) print(pascal(col, row) + " ") println() } println("pascal(0,1)-->" + pascal(0, 1)) println("pascal(0,1)-->" + pascal(0, 1)) println("pascal(0,1)-->" + pascal(0, 1)) } /** * @author Umashankar * pascal triangle * https://j2eedev.org */ def pascal(c: Int, r: Int): Int = if (c == r || c == 0) 1 else pascal(c - 1, r - 1) + pascal(c, r - 1) } |