combinatorics.nbt 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. use core::error
  2. use core::functions
  3. use core::numbers
  4. use math::transcendental
  5. @name("Factorial")
  6. @description("The product of the integers 1 through n. Numbat also supports calling this via the postfix operator `n!`.")
  7. @url("https://en.wikipedia.org/wiki/Factorial")
  8. @example("factorial(4)")
  9. @example("4!")
  10. fn factorial(n: Scalar) -> Scalar = n!
  11. @name("Falling factorial")
  12. @description("Equal to $n⋅(n-1)⋅…⋅(n-k+2)⋅(n-k+1)$ (k terms total). If n is an integer, this is the number of k-element permutations from a set of size n. k must always be an integer.")
  13. @url("https://en.wikipedia.org/wiki/Falling_and_rising_factorials")
  14. @example("falling_factorial(4, 2)")
  15. fn falling_factorial(n: Scalar, k: Scalar) -> Scalar =
  16. if k < 0 || !is_integer(k) then
  17. error("in falling_factorial(n, k), k must be a nonnegative integer")
  18. else if is_zero(k) then
  19. 1
  20. else
  21. n * falling_factorial(n-1, k-1)
  22. @name("Binomial coefficient")
  23. @description("Equal to falling_factorial(n, k)/k!, this is the coefficient of $x^k$ in the series expansion of $(1+x)^n$ (see “binomial series”). If n is an integer, then this this is the number of k-element subsets of a set of size n, often read \"n choose k\". k must always be an integer.")
  24. @url("https://en.wikipedia.org/wiki/Binomial_coefficient")
  25. @example("binom(5, 2)")
  26. fn binom(n: Scalar, k: Scalar) -> Scalar =
  27. if !is_integer(k) then
  28. error("in binom(n, k), k must be an integer")
  29. else if k < 0 || (k > n && is_integer(n)) then
  30. 0
  31. else
  32. falling_factorial(n, k) / k!
  33. @name("Fibonacci numbers")
  34. @description("The nth Fibonacci number, where n is a nonnegative integer. The Fibonacci sequence is given by $F_0=0$, $F_1=1$, and $F_n=F_{{n-1}}+F_{{n-2}}$ for $n≥2$. The first several elements, starting with $n=0$, are $0, 1, 1, 2, 3, 5, 8, 13$.")
  35. @url("https://en.wikipedia.org/wiki/Fibonacci_sequence")
  36. @example("fibonacci(5)")
  37. fn fibonacci(n: Scalar) -> Scalar =
  38. if !(is_integer(n) && n >= 0) then
  39. error("the argument to fibonacci(n) must be a nonnegative integer")
  40. else
  41. # use Binet's formula for constant time
  42. round((phi^n - (-phi)^(-n))/sqrt(5))
  43. where phi = (1+sqrt(5))/2
  44. @name("Lucas numbers")
  45. @description("The nth Lucas number, where n is a nonnegative integer. The Lucas sequence is given by $L_0=2$, $L_1=1$, and $L_n=L_{{n-1}}+L_{{n-2}}$ for $n≥2$. The first several elements, starting with $n=0$, are $2, 1, 3, 4, 7, 11, 18, 29$.")
  46. @url("https://en.wikipedia.org/wiki/Lucas_number")
  47. @example("lucas(5)")
  48. fn lucas(n: Scalar) -> Scalar =
  49. if !(is_integer(n) && n >= 0) then
  50. error("the argument to lucas(n) must be a nonnegative integer")
  51. else
  52. # use Binet's formula for constant time
  53. round(phi^n + (1-phi)^n)
  54. where phi = (1+sqrt(5))/2
  55. @name("Catalan numbers")
  56. @description("The nth Catalan number, where n is a nonnegative integer. The Catalan sequence is given by $C_n=\frac{{1}}{{n+1}}\binom{{2n}}{{n}}=\binom{{2n}}{{n}}-\binom{{2n}}{{n+1}}$. The first several elements, starting with $n=0$, are $1, 1, 2, 5, 14, 42, 132, 429$.")
  57. @url("https://en.wikipedia.org/wiki/Catalan_number")
  58. @example("catalan(5)")
  59. fn catalan(n: Scalar) -> Scalar =
  60. if !(is_integer(n) && n >= 0) then
  61. error("the argument to catalan(n) must be a nonnegative integer")
  62. else
  63. binom(2*n, n) / (n+1)