Skip to content

Instantly share code, notes, and snippets.

@mohnoor94
Last active March 3, 2018 21:58
Show Gist options
  • Save mohnoor94/4f3880788dd3bdc5427a1ee266a7e3f1 to your computer and use it in GitHub Desktop.
Save mohnoor94/4f3880788dd3bdc5427a1ee266a7e3f1 to your computer and use it in GitHub Desktop.
Functional Programming Principles in Scala - Exercise 1: Pascal’s Triangle
object Main {
def main(args: Array[String]) {
println("Pascal's Triangle")
for (row <- 0 to 10) {
for (col <- 0 to row)
print(pascal(col, row) + " ")
println()
}
}
/**
* Exercise 1
*/
def pascal(c: Int, r: Int): Int =
if (c == 0 || c == r) 1
else pascal(c - 1, r - 1) + pascal(c, r - 1)
}
@mohnoor94
Copy link
Author

This exercise is copied from the Functional Programming Principles in Scala course, week 1. Find more on my website.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment