1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// webrust/src/math/mod.rs
//! # Combinatorics Functions
//!
//! High-performance combinatorial calculations with overflow-safe algorithms.
//! Uses single-letter names matching mathematical notation.
//!
//! ## Functions
//!
//! - `C(n, k)` - **Combinations** (n choose k) : C(n,k) = n! / (k!(n-k)!)
//! - `A(n, k)` - **Arrangements** (n arrange k) : A(n,k) = n! / (n-k)!
//! - `P(n)` - **Permutations** (n factorial) : P(n) = n!
//!
//! All functions return `u64` and use optimized algorithms to minimize
//! intermediate overflow risks.
//!
//! ## Examples
//!
//! ### Combinations (C)
//! ```rust
//! use webrust::prelude::*;
//! # fn example() {
//! // How many ways to choose 2 items from 5?
//! let ways = C(5, 2); // 10
//! assert_eq!(ways, 10);
//!
//! // Pascal's triangle
//! let row: Vec<u64> = 0.to(6).then(|k| C(5, k));
//! // [1, 5, 10, 10, 5, 1]
//! # }
//! ```
//!
//! ### Arrangements (A)
//! ```rust
//! use webrust::prelude::*;
//! # fn example() {
//! // How many ways to arrange 3 items from 5?
//! let ways = A(5, 3); // 60
//! assert_eq!(ways, 60);
//!
//! // Podium positions from 8 runners
//! let podiums = A(8, 3); // 336
//! # }
//! ```
//!
//! ### Permutations (P)
//! ```rust
//! use webrust::prelude::*;
//! # fn example() {
//! // How many ways to arrange 5 items?
//! let ways = P(5); // 120
//! assert_eq!(ways, 120);
//!
//! // Permutations for larger sets
//! let large = P(10); // 3,628,800
//! # }
//! ```
//!
//! ### Real-World Example: Pascal's Triangle
//! ```rust
//! use webrust::prelude::*;
//! # #[gui] fn example() {
//! println("<darkred b mt25>Pascal's Triangle");
//! let triangle: Vec<Vec<u64>> = 0.to(10).then(|n| {
//! 0.to(n + 1).then(|k| C(n, k))
//! });
//! table(&triangle);
//! # }
//! ```
//!
//! ## Algorithm Details
//!
//! - **C(n, k)**: Uses symmetry property C(n,k) = C(n,n-k) and
//! incremental multiplication/division to avoid factorial overflow
//! - **A(n, k)**: Direct product avoiding full factorial computation
//! - **P(n)**: Simple factorial with sequential multiplication
//!
//! ## Performance
//!
//! All functions are `#[inline]` and compile to tight loops.
//! Time complexity: O(k) for C and A, O(n) for P.