    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
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292

    use std::collections::HashSet;

    /// Finds all factor pairs for a positive integer `n`.
    ///
    /// # Challenge
    ///
    /// Write a program that finds all the factor pairs for a number `n`.
    ///
    /// # Description
    ///
    /// Generates a `HashSet` of factor pairs for a positive integer `n`.
    ///
    /// This function calculates and returns a `HashSet` containing all unique factor pairs
    /// of the input positive integer `n`. A factor pair is a pair of positive integers
    /// `(a, b)` where `a` and `b` are both factors of `n` (i.e., `a * b == n`).
    ///
    /// # Arguments
    ///
    /// * `n` - The positive integer for which factor pairs are to be calculated.
    ///
    /// # Returns
    ///
    /// A `HashSet` containing all unique factor pairs of the input integer `n`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::collections::HashSet;
    /// use ladderz::pre_algebra::unit1::get_factor_pairs;
    ///
    /// fn main() {
    ///     let result_pairs = get_factor_pairs(12);
    ///     let expected_pairs: HashSet<(u32, u32)> = [(1, 12), (2, 6), (3, 4)].into();
    ///     assert_eq!(result_pairs, expected_pairs);
    /// }
    /// ```
    ///
    /// # Note
    ///
    /// This function calculates factor pairs by iterating through positive integers from 1 to `n`
    /// (inclusive) and checking if they divide `n` evenly. If they do, a factor pair `(a, b)` is
    /// added to the `HashSet`. The function ensures that factor pairs are unique, so `(a, b)` and
    /// `(b, a)` will not both appear in the set.
    pub fn get_factor_pairs(n: u32) -> HashSet<(u32, u32)> {
        let mut factor_pairs: HashSet<(u32, u32)> = HashSet::new();

        for num in 1..n + 1 {
            let dividend: u32 = n;
            let divisor: u32 = num;
            let quotient: u32 = dividend / num;
            let remainder: u32 = dividend % num;
            if remainder == 0 && !factor_pairs.contains(&(quotient, divisor)) {
                factor_pairs.insert((divisor, quotient));
            }
        }

        factor_pairs
    }

    /// Finds all factors of a positive integer `n`.
    ///
    /// # Challenge
    ///
    /// Write a program that finds all the factors of a number. Assume that `n` is a positive integer greater than or equal to 1.
    ///
    /// # Description
    ///
    /// Generates a `HashSet` of factors for a positive integer `n`.
    ///
    /// This function calculates and returns a `HashSet` containing all unique factors
    /// of the input positive integer `n`. A factor of `n` is a positive integer `a` where
    /// `n` is evenly divisible by `a` (i.e., `n % a == 0`).
    ///
    /// # Arguments
    ///
    /// * `n` - The positive integer for which factors are to be calculated.
    ///
    /// # Returns
    ///
    /// A `HashSet` containing all unique factors of the input integer `n`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::collections::HashSet;
    /// use ladderz::pre_algebra::unit1::get_factors;
    ///
    /// fn main() {
    ///     let result_factors = get_factors(16);
    ///     let expected_factors: HashSet<u32> = [1, 2, 4, 8, 16].into();
    ///     assert_eq!(result_factors, expected_factors);
    /// }
    /// ```
    ///
    /// # Note
    ///
    /// This function calculates factors by iterating through positive integers from 1 to `n`
    /// (inclusive) and checking if they divide `n` evenly. If they do, the factor is added to
    /// the `HashSet`. The function ensures that factors are unique, so duplicates are not added.
    pub fn get_factors(n: u32) -> HashSet<u32> {
        let mut factors: HashSet<u32> = HashSet::new();

        for num in 1..n + 1 {
            if n % num == 0 {
                factors.insert(num);
            }
        }
        factors
    }

    /// Checks if a positive integer `x` is a factor of another positive integer `y`.
    ///
    /// # Challenge
    ///
    /// Write a program that determines whether one positive integer is a factor of another.
    ///
    /// # Description
    ///
    /// Checks if a positive integer `x` is a factor of another positive integer `y`.
    ///
    /// A factor of `y` is a positive integer `x` where `y` is evenly divisible by `x` (i.e., `y % x == 0`).
    ///
    /// # Arguments
    ///
    /// * `x` - The positive integer to determine whether it is a factor of `y` or not.
    /// * `y` - The positive integer for which the factor check of `x` is performed.
    ///
    /// # Returns
    ///
    /// `true` if `x` is a factor of `y`, `false` otherwise.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ladderz::pre_algebra::unit1::is_factor;
    ///
    /// fn main() {
    ///     assert!(is_factor(2, 16)); // 2 is a factor of 16
    ///     assert!(!is_factor(3, 16)); // 3 is not a factor of 16
    /// }
    /// ```
    ///
    /// # Note
    ///
    /// This function determines if `x` is a factor of `y` by checking if `y` is evenly divisible by `x`
    /// (i.e., `y % x == 0`).
    pub fn is_factor(x: u32, y: u32) -> bool {
        y % x == 0
    }

    /// Checks if a positive integer `x` is a multiple of another positive integer `y`.
    ///
    /// # Challenge
    ///
    /// Write a program that determines whether one positive integer is a multiple of another.
    ///
    /// # Description
    ///
    /// Checks if a positive integer `x` is a multiple of another positive integer `y`.
    ///
    /// A multiple of `y` is a positive integer `x` where `x` is evenly divisible by `y` (i.e., `x % y == 0`).
    ///
    /// # Arguments
    ///
    /// * `x` - The positive integer to determine whether it is a multiple of `y` or not.
    /// * `y` - The positive integer for which the multiple check of `x` is performed.
    ///
    /// # Returns
    ///
    /// `true` if `x` is a multiple of `y`, `false` otherwise.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ladderz::pre_algebra::unit1::is_multiple;
    ///
    /// fn main() {
    ///     assert!(is_multiple(16, 2)); // 16 is a multiple of 2
    ///     assert!(!is_multiple(16, 3)); // 16 is not a multiple of 3
    /// }
    /// ```
    pub fn is_multiple(x: u32, y: u32) -> bool {
        x % y == 0
    }

    /// Finds all the multiples of a positive integer `n` up to and including `end` (in the range [n, end]).
    ///
    /// # Challenge
    ///
    /// Write a program that finds all the multiples of a positive integer `n` in a given range.
    ///
    /// # Description
    ///
    /// Returns a HashSet containing all the multiples of a positive integer `n` in the range [n, end].
    ///
    /// A multiple of `n` is a positive integer `num` where `num` is evenly divisible by `n` (i.e., `num % n == 0`).
    ///
    /// # Arguments
    ///
    /// * `n` - The positive integer for which multiples are to be found.
    /// * `end` - The upper limit of the range for finding multiples.
    ///
    /// # Returns
    ///
    /// A HashSet containing all the multiples of `n` in the range [n, end].
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ladderz::pre_algebra::unit1::get_multiples_in_range;
    /// use std::collections::HashSet;
    ///
    /// fn main() {
    ///     let result: HashSet<u32> = get_multiples_in_range(2, 10);
    ///     let expected: HashSet<u32> = [2, 4, 6, 8, 10].into();
    ///     assert_eq!(result, expected);
    ///
    ///     let result: HashSet<u32> = get_multiples_in_range(3, 15);
    ///     let expected: HashSet<u32> = [3, 6, 9, 12, 15].into();
    ///     assert_eq!(result, expected);
    /// }
    /// ```
    pub fn get_multiples_in_range(n: u32, end: u32) -> HashSet<u32> {
        let mut multiples: HashSet<u32> = HashSet::new();

        for num in n..end + 1 {
            if num % n == 0 {
                multiples.insert(num);
            }
        }
        multiples
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn test_get_factor_pairs() {
            let result: HashSet<(u32, u32)> = get_factor_pairs(1);
            let expected: HashSet<(u32, u32)> = [(1, 1)].into();
            assert_eq!(result, expected);

            let result_2: HashSet<(u32, u32)> = get_factor_pairs(16);
            let expected_2: HashSet<(u32, u32)> = [(1, 16), (2, 8), (4, 4)].into();
            assert_eq!(result_2, expected_2);
        }

        #[test]
        fn test_get_factors() {
            let result: HashSet<u32> = get_factors(1);
            let expected: HashSet<u32> = [1].into();
            assert_eq!(result, expected);

            let result_2: HashSet<u32> = get_factors(16);
            let expected_2: HashSet<u32> = [1, 2, 4, 8, 16].into();
            assert_eq!(result_2, expected_2);
        }

        #[test]
        fn test_is_factor() {
            let result: bool = true;
            let expected: bool = is_factor(2, 10);
            assert_eq!(result, expected);

            let result_2: bool = false;
            let expected_2: bool = is_factor(3, 10);
            assert_eq!(result_2, expected_2);
        }

        #[test]
        fn test_is_multiple() {
            let result: bool = true;
            let expected: bool = is_multiple(10, 2);
            assert_eq!(result, expected);

            let result_2: bool = false;
            let expected_2: bool = is_multiple(11, 2);
            assert_eq!(result_2, expected_2);
        }

        #[test]
        fn test_get_multiples_in_range() {
            let result: HashSet<u32> = get_multiples_in_range(2, 20);
            let expected: HashSet<u32> = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20].into();
            assert_eq!(result, expected);

            let result_2: HashSet<u32> = get_multiples_in_range(5, 34);
            let expected_2: HashSet<u32> = [5, 10, 15, 20, 25, 30].into();
            assert_eq!(result_2, expected_2);
        }
    }