nChooseK: Computes 'n choose k'

Description Usage Arguments Value Author(s) References See Also Examples

View source: R/nChooseK.R

Description

Computes "n choose k", where n is any real number and k is any integer. "n choose k" is equal to n!/((n-k)!k!).

If argument n and/or argument k is a vector, they will be replicated such that they have the same length.

If "n choose k" is a too large number to be represented, the logarithm of "n choose k" can be calculated by setting log to TRUE or to any integer, which is then specifying the base of the logarithm.

Usage

1

Arguments

n

The number of items t"n choose k"o choose from.

k

The number of items to choose.

log

The base used for the logarithm of the values returned. If log=TRUE the natural base (exp(1)) will be used. If log=FALSE or log=NULL the non-logged value is returned.

Value

Returns a positive integer greater or equal to one that is the greatest common divider between the two given values.

Author(s)

Henrik Bengtsson

References

[1] Alexander Bogomolny, Euclid's Algorithm, Feb 2003, http://www.cut-the-knot.com/blue/Euclid.shtml [2] R-help thread, About 'choose' function, November 7, 2004.

See Also

choose() and lchoose() (Special). factorial(). See Special for information about gamma() and lgamma(), which are used for calculating the factorials.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
n <- 4; k <- 2;
print( nChooseK(n,k) )   # 6

n <- 6; k <- 2;
print( nChooseK(n,k) )   # 15

n <- 6; k <- 3;
print( nChooseK(n,k) )   # 20

# (a+b)^2 = 1*a^2 + 2*a*b + 1*b^2
n <- 2; k <- 0:2;
print( nChooseK(n,k) )   # 1 2 1

# (a+b)^3 = 1*a^3 + 3*a^2*b + 3*a*b^2 + 1*b^3
n <- 3; k <- 0:3;
print( nChooseK(n,k) )   # 1 3 3 1

HenrikBengtsson/R.basic documentation built on May 6, 2019, 11:51 p.m.