RosettaCodeData/Task/Evaluate-binomial-coefficients/Gambas/evaluate-binomial-coefficients.gambas
2025-06-11 20:16:52 -04:00

36 lines
634 B
Text

Public Sub Main()
For n As Integer = 0 To 14
For k As Integer = 0 To n
Print Format(binomial(n, k), " ####");
Next
Print
Next
End
Function factorial(n As Integer) As Integer
If n < 1 Then Return 1
Dim product As Float = 1 ' Float to avoid overflow
For i As Integer = 2 To n
product *= i
Next
Return product
End Function
Function binomial(n As Integer, k As Integer) As Integer
If n < 0 Or k < 0 Or n <= k Then Return 1
Dim product As Float = 1 ' Float to avoid overflow
For i As Integer = n - k + 1 To n
product *= i
Next
Return Int(product / factorial(k))
End Function