Sign in to follow this  
Followers 0
mikehunt114

Math functions

1 post in this topic

#1 ·  Posted (edited)

Functions for calculating factorials and binomial coefficients (aka choose):

Func _MathFactorial($number)
    If Not IsNumber($number) Then
        Return 0
        SetError(1)
    EndIf
    Local $value = 1
    For $i = 1 To $number
        $value *= $i
    Next
    Return $value
EndFunc

Func _MathChoose($num1, $num2)      ;Important: $num1 choose $num2
    If Not IsNumber($num1) And Not IsNumber($num2) Then
        Return 0
        SetError(1)
    EndIf
    If $num2 < 0 Or $num2 > $num1 Then Return 0
    Local $num1_Factorial = _MathFactorial($num1)
    Local $num2_Factorial = _MathFactorial($num2)
    Local $diff_Factorial = _MathFactorial($num1 - $num2)
    Local $value = $num1_Factorial/($num2_Factorial*$diff_Factorial)
    Return $value   
EndFunc

Edit: Added restraints for _MathChoose

Edited by mikehunt114

IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0