# Combinations and Permutaions

## Recommended Posts

For use with my statistics class, I whipped up 3 functions for Combinations and Permutations

The Functions;

```Func _nCr(\$n_Population, \$n_Permutation)
Local \$top = _Factorial(\$n_Population)
local \$SubBottom1 = _Factorial(\$n_Permutation)
local \$SubBottom2 = _Factorial(\$n_Population-\$n_Permutation)
Local \$Bottom = \$SubBottom1*\$SubBottom2
Local \$result = Round(\$top/\$bottom,0)
Return \$result
EndFunc

Func _nPr(\$n_Population, \$n_Permutation)
local \$top = _Factorial(\$n_Population)
local \$Bottom = _Factorial(\$n_Population-\$n_Permutation)
Local \$result = \$top/\$bottom
Return Round(\$top/\$bottom,0)
EndFunc

Func _Factorial(\$n_Number)
Local \$result = 1
For \$i = \$n_Number to 1 step -1
\$result = \$result*\$i
Next
Return \$result

Unfortunately, for some reason (I believe it is a restriction in autoit) I am unable to produce and accurate Factorial (equal to the "!" in math) for any number greater than 20

anyway, just more math!

##### Share on other sites

Does anyone have an idea how to get around the problem with factorials over 20?

##### Share on other sites

Does anyone have an idea how to get around the problem with factorials over 20?

Hi Paulie,

this should fix your problem.

```Func _Factorial(\$n_Number)
Local \$result = 1.0 ;<== my modification
For \$i = \$n_Number to 1 step -1
\$result = \$result*\$i
Next
Return \$result
EndFunc```

##### Share on other sites

Awesome Paulie and Cubehead, this is just the solution I was looking for. Thanks!

##### Share on other sites

At least this one is only 7 years old...

• 2

√-1 2^3 ∑ π, and it was delicious!

## 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

×

• Wiki

• Back

• Git

## Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.