For example in prize drawings you might have pick 5 numbers (order does not matter) from 1-45
How would you calculate the possible combinations?
Number of possible sets within a given number range Topic is solved
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Number of possible sets within a given number range
Not an AHK question or issue.
45!/5!/40! = 1,221,759
45!/5!/40! = 1,221,759
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Number of possible sets within a given number range
OK, have fun with it!
Formulas are on the Web if you need them; search for permutations and combinations. I imagine that some people have also already written AHK scripts for this (could search the forum here). Computing a factorial is probably achievable in just a few lines.
Also: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=85788
Formulas are on the Web if you need them; search for permutations and combinations. I imagine that some people have also already written AHK scripts for this (could search the forum here). Computing a factorial is probably achievable in just a few lines.
Also: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=85788
Re: Number of possible sets within a given number range Topic is solved
The function for factorial itself is indeed short. The problem is you can't just simply calculate the factorial of everything because the numbers get too large. However, we can take advantage of a lot of canceling in the fraction (e.g., 45! / 40! = 45 x 44 x 43 x 42 x 41) to keep the numbers smaller:
Code: Select all
MsgBox, % Combinations(45, 5)
return
Combinations(n, k) {
return NFactDivNMinKFact(n, k) / Factorial(k)
}
Factorial(n) {
return n < 3 ? n : n * Factorial(n - 1)
}
NFactDivNMinKFact(n, k) {
f := n
loop, % k - 1
f := f * (n - A_Index)
return f
}
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Number of possible sets within a given number range
thanks, I was using this to test http://www.webmath.com/lottery.html your function is exactly like that, I just added round.boiler wrote: ↑19 Jan 2021, 15:37The function for factorial itself is indeed short. The problem is you can't just simply calculate the factorial of everything because the numbers get too large. However, we can take advantage of a lot of canceling in the fraction (e.g., 45! / 40! = 45 x 44 x 43 x 42 x 41) to keep the numbers smaller:Code: Select all
MsgBox, % Combinations(45, 5) return Combinations(n, k) { return NFactDivNMinKFact(n, k) / Factorial(k) } Factorial(n) { return n < 3 ? n : n * Factorial(n - 1) } NFactDivNMinKFact(n, k) { f := n loop, % k - 1 f := f * (n - A_Index) return f }
Code: Select all
MsgBox, % Round(Combinations(35, 5))
Re: Number of possible sets within a given number range
No problem. You can just make the function round it so you just simply call the function, like this:
Code: Select all
MsgBox, % Combinations(45, 5)
return
Combinations(n, k) {
return Round(NFactDivNMinKFact(n, k) / Factorial(k))
}
Factorial(n) {
return n < 3 ? n : n * Factorial(n - 1)
}
NFactDivNMinKFact(n, k) {
f := n
loop, % k - 1
f := f * (n - A_Index)
return f
}