number_utils/number_utils.rs
1use crate::impl_number_utils_for;
2use std::marker::Sized;
3
4impl_number_utils_for!(u32, u64, u128, usize);
5
6pub trait NumberUtils {
7 /// Calculates factorial.
8 ///
9 /// # Examples
10 /// Basic usage:
11 /// ```
12 /// # use number_utils::NumberUtils;
13 /// # fn main() {
14 /// let n: u32 = 6;
15 /// assert_eq!(n.factorial(), 720);
16 /// # }
17 /// ```
18 fn factorial(&self) -> Self;
19
20 /// Calculates k-permutations.
21 ///
22 /// # Examples
23 /// Basic usage:
24 /// ```
25 /// # use number_utils::NumberUtils;
26 /// # fn main() {
27 /// let n: u32 = 6;
28 /// assert_eq!(n.permutation(3), 120);
29 /// # }
30 /// ```
31 fn permutation(&self, k: u32) -> Self;
32
33 /// Calculates k-combination.
34 ///
35 /// # Examples
36 /// Basic usage:
37 /// ```
38 /// # use number_utils::NumberUtils;
39 /// # fn main() {
40 /// let n: u32 = 6;
41 /// assert_eq!(n.combination(3), 20);
42 /// # }
43 /// ```
44 fn combination(&self, k: u32) -> Self;
45
46 /// Calculates greatest common divisor of two integers.
47 ///
48 /// # Examples
49 /// Basic usage:
50 /// ```
51 /// # use number_utils::NumberUtils;
52 /// # fn main() {
53 /// let n: u32 = 54;
54 /// assert_eq!(n.gcd(24), 6);
55 /// # }
56 /// ```
57 fn gcd(&self, m: Self) -> Self;
58
59 /// Calculates least common multiple.
60 ///
61 /// # Examples
62 /// Basic usage:
63 /// ```
64 /// # use number_utils::NumberUtils;
65 /// # fn main() {
66 /// let n: u32 = 72;
67 /// assert_eq!(n.lcm(10), 360);
68 /// # }
69 fn lcm(&self, m: Self) -> Self;
70
71 /// Calculates integer square root.
72 ///
73 /// # Examples
74 /// Basic usage:
75 /// ```
76 /// # use number_utils::NumberUtils;
77 /// # fn main() {
78 /// let n: u32 = 122;
79 /// assert_eq!(n.isqrt(), 11);
80 /// # }
81 /// ```
82 fn isqrt(&self) -> Self;
83
84 /// Calculates factorial, returning None if overflow occured.
85 ///
86 /// # Examples
87 /// Basic usage:
88 /// ```
89 /// # use number_utils::NumberUtils;
90 /// # fn main() {
91 /// let n: u32 = 5;
92 /// assert_eq!(n.checked_factorial(), Some(120));
93 /// assert_eq!(u32::MAX.checked_factorial(), None);
94 /// # }
95 /// ```
96 fn checked_factorial(&self) -> Option<Self>
97 where
98 Self: Sized;
99
100 /// Calculates k-permutations, returning None if overflow occured.
101 ///
102 /// # Examples
103 /// Basic usage:
104 /// ```
105 /// # use number_utils::NumberUtils;
106 /// # fn main() {
107 /// let n: u32 = 6;
108 /// assert_eq!(n.checked_permutation(3), Some(120));
109 /// assert_eq!(u32::MAX.checked_permutation(3), None);
110 /// # }
111 /// ```
112 fn checked_permutation(&self, k: u32) -> Option<Self>
113 where
114 Self: Sized;
115
116 /// Calculates k-combination, returning None if overflow occured.
117 ///
118 /// # Examples
119 /// Basic usage:
120 /// ```
121 /// # use number_utils::NumberUtils;
122 /// # fn main() {
123 /// let n: u32 = 6;
124 /// assert_eq!(n.checked_combination(3), Some(20));
125 /// assert_eq!(u32::MAX.checked_combination(1200), None);
126 /// # }
127 /// ```
128 fn checked_combination(&self, k: u32) -> Option<Self>
129 where
130 Self: Sized;
131
132 /// Calculates number of digits.
133 ///
134 /// # Examples
135 /// ```
136 /// # use number_utils::NumberUtils;
137 /// # fn main() {
138 /// let n: u32 = 1234567890;
139 /// assert_eq!(n.digits(), 10);
140 /// # }
141 /// ```
142 fn digits(&self) -> Self;
143}
144
145/// Implementation of the Pascal's triangle.
146///
147/// # Examples
148/// ```
149/// # use number_utils::pascals_triangle;
150/// # fn main() {
151/// assert_eq!(pascals_triangle(3), vec![1, 1, 1, 1, 2, 1]);
152/// # }
153/// ```
154pub fn pascals_triangle(n: usize) -> Vec<usize> {
155 let mut result = vec![1];
156 for i in 1..n {
157 let r = result.len() - i;
158 result.push(1);
159 for j in 1..i {
160 result.push(result[r + j - 1] + result[r + j]);
161 }
162 result.push(1);
163 }
164 result
165}