diff --git a/ladderz/all.html b/ladderz/all.html index cd04d4b..b0b0017 100644 --- a/ladderz/all.html +++ b/ladderz/all.html @@ -1 +1 @@ -
pub fn get_multiples_in_range(n: u32, end: u32) -> HashSet<u32>Finds all the multiples of a positive integer n up to and including end (in the range [n, end]).
Write a program that finds all the multiples of a positive integer n in a given range.
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).
n - The positive integer for which multiples are to be found.end - The upper limit of the range for finding multiples.A HashSet containing all the multiples of n in the range [n, end].
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);
+}Factors and multiples
-n.n.x is a factor of another positive integer y.x is a multiple of another positive integer y.Factors and multiples
+n.n.n up to and including end (in the range [n, end]).x is a factor of another positive integer y.x is a multiple of another positive integer y.n.","Finds all factors of a positive integer n.","Checks if a positive integer x is a factor of another …","Checks if a positive integer x is a multiple of another …"],"i":[0,0,0,0,0,0],"f":[0,0,[1,2],[1,[[2,[1]]]],[[1,1],3],[[1,1],3]],"c":[],"p":[[15,"u32"],[3,"HashSet"],[15,"bool"]]}\
+"ladderz":{"doc":"Implementations of mathematical and technical concepts in …","t":"AAFFFFF","n":["pre_algebra","unit1","get_factor_pairs","get_factors","get_multiples_in_range","is_factor","is_multiple"],"q":[[0,"ladderz"],[1,"ladderz::pre_algebra"],[2,"ladderz::pre_algebra::unit1"]],"d":["Various pre-algebra implementations including factor …","Factors and multiples","Finds all factor pairs for a positive integer n.","Finds all factors of a positive integer n.","Finds all the multiples of a positive integer n up to and …","Checks if a positive integer x is a factor of another …","Checks if a positive integer x is a multiple of another …"],"i":[0,0,0,0,0,0,0],"f":[0,0,[1,2],[1,[[2,[1]]]],[[1,1],[[2,[1]]]],[[1,1],3],[[1,1],3]],"c":[],"p":[[15,"u32"],[3,"HashSet"],[15,"bool"]]}\
}');
if (typeof window !== 'undefined' && window.initSearch) {window.initSearch(searchIndex)};
if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
diff --git a/src/ladderz/pre_algebra/unit1.rs.html b/src/ladderz/pre_algebra/unit1.rs.html
index 9ae985b..d7899e9 100644
--- a/src/ladderz/pre_algebra/unit1.rs.html
+++ b/src/ladderz/pre_algebra/unit1.rs.html
@@ -231,6 +231,65 @@
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`.
@@ -416,6 +475,54 @@
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::*;
@@ -463,5 +570,16 @@
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);
+ }
}