I have created a function which returns the number that corresponds to the given row and column from the pascal triangle. I have used the formula for this:
n! / r! * (r-n)!
The code:
def pascal(c: Int, r: Int): Int = {
def factorial(number: Int): Int = {
def loop(adder: Int, num: Int): Int = {
if( num == 0) adder
else loop(adder * num, num - 1)
}
loop(1,number)
}
factorial(r) / (factorial(c) * factorial(r-c));
}
I received 180 points out of 190 for this function because it returns an arithmetic exception for a given case (they don't give me the case). How could I improve this code and how can I make it better? Most importantly, how can I spot the bug?