combin
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
// @description The combin function is a the combination function 
//	 as it calculates the number of possible combinations for two given numbers.
//	 This function takes two arguments: the number and the number_chosen. 
//	 For example, if the number is 5 and the number chosen is 1, 
//	 there are 5 combinations, giving 5 as a result.
// reference:
// 		https://ideone.com/aDJXNO
//      https://support.microsoft.com/en-us/office/combin-function-12a3f276-0a21-423a-8de6-06990aaf638a
library("combin")

// @function Returns the number of combinations for a given number of items. Use to determine the total possible number of groups for a given number of items.
// @param n int, The number of items.
// @param kin int, The number of items in each combination.
// @returns int.
export combin(int n, int kin)=>
    int out = 0
    int k = kin
    if k > n 
        out := 0
    else
        if (k * 2) > n
            k := n - k
        if (k == 0) 
            out := 1
        else
            int result = n 
			int i = 2
			while i <= k
                result *= n - i + 1
                result /= i
				i += 1
            out := result
    out

// example usage; "n choose kin"
out = combin(100, 3)
plot(out)