Number of possible sets within a given number range Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Number of possible sets within a given number range

Post by AHKStudent » 19 Jan 2021, 13:16

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?
User avatar
mikeyww
Posts: 26894
Joined: 09 Sep 2014, 18:38

Re: Number of possible sets within a given number range

Post by mikeyww » 19 Jan 2021, 13:20

Not an AHK question or issue.

45!/5!/40! = 1,221,759
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Number of possible sets within a given number range

Post by AHKStudent » 19 Jan 2021, 14:45

mikeyww wrote:
19 Jan 2021, 13:20
Not an AHK question or issue.

45!/5!/40! = 1,221,759
I am looking to build it using a script, i'd never post random questions here. :thumbup:
User avatar
mikeyww
Posts: 26894
Joined: 09 Sep 2014, 18:38

Re: Number of possible sets within a given number range

Post by mikeyww » 19 Jan 2021, 15:02

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
User avatar
boiler
Posts: 16934
Joined: 21 Dec 2014, 02:44

Re: Number of possible sets within a given number range  Topic is solved

Post by boiler » 19 Jan 2021, 15:37

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
}
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Number of possible sets within a given number range

Post by AHKStudent » 19 Jan 2021, 16:44

boiler wrote:
19 Jan 2021, 15:37
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
}
thanks, I was using this to test http://www.webmath.com/lottery.html your function is exactly like that, I just added round.

Code: Select all

MsgBox, % Round(Combinations(35, 5))
thank you
User avatar
boiler
Posts: 16934
Joined: 21 Dec 2014, 02:44

Re: Number of possible sets within a given number range

Post by boiler » 19 Jan 2021, 18:18

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
}
Post Reply

Return to “Ask for Help (v1)”