diff --git a/help.html b/help.html index a0c7e70..7a2b8e9 100644 --- a/help.html +++ b/help.html @@ -1 +1 @@ -
Non-production implementations of mathematical and technical concepts in Rust.
+Implementations of mathematical and technical concepts in Rust.
Various pre-algebra implementations including multiples (planned), factor pairs, etc.
+Various pre-algebra implementations including multiples (planned), factor pairs, etc.
pub fn get_factor_pairs(n: u32) -> HashSet<(u32, u32)>Generates a HashSet of factor pairs for a given positive integer n.
pub fn get_factor_pairs(n: u32) -> HashSet<(u32, u32)>Finds all factor pairs for a given positive integer.
+Write a program that finds all the factor pairs for a given number n.
Generates a HashSet of factor pairs for a given 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).
pub fn get_factors(n: u32) -> HashSet<u32>Finds all factors of a given positive integer.
+Write a program that finds all the factors of a given number. Assume that n is a positive integer greater than or equal to 1.
Generates a HashSet of factors for a given 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).
n - The positive integer for which factors are to be calculated.A HashSet containing all unique factors of the input integer n.
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);
+}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.
Factors and multiples
-HashSet of factor pairs for a given positive integer n.Factors and multiples
+HashSet of factor pairs for a given positive …"],"i":[0,0,0],"f":[0,0,[1,2]],"c":[],"p":[[15,"u32"],[3,"HashSet"]]}\
+"ladderz":{"doc":"Implementations of mathematical and technical concepts in …","t":"AAFF","n":["pre_algebra","unit1","get_factor_pairs","get_factors"],"q":[[0,"ladderz"],[1,"ladderz::pre_algebra"],[2,"ladderz::pre_algebra::unit1"]],"d":["Various pre-algebra implementations including multiples …","Factors and multiples","Finds all factor pairs for a given positive integer.","Finds all factors of a given positive integer."],"i":[0,0,0,0],"f":[0,0,[1,2],[1,[[2,[1]]]]],"c":[],"p":[[15,"u32"],[3,"HashSet"]]}\
}');
if (typeof window !== 'undefined' && window.initSearch) {window.initSearch(searchIndex)};
if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
diff --git a/settings.html b/settings.html
index 14dd325..85e7a4d 100644
--- a/settings.html
+++ b/settings.html
@@ -1 +1 @@
-1 +lib.rs - source //! Implementations of mathematical and technical concepts in Rust. /// Various pre-algebra implementations including multiples (planned), factor pairs, etc. pub mod pre_algebra; diff --git a/src/ladderz/pre_algebra.rs.html b/src/ladderz/pre_algebra.rs.html index 78748c8..3c783cf 100644 --- a/src/ladderz/pre_algebra.rs.html +++ b/src/ladderz/pre_algebra.rs.html @@ -1,4 +1,4 @@ -pre_algebra.rs - source 1 +pre_algebra.rs - source /// Factors and multiples pub mod unit1; diff --git a/src/ladderz/pre_algebra/unit1.rs.html b/src/ladderz/pre_algebra/unit1.rs.html index d7fdb4e..b8ba17c 100644 --- a/src/ladderz/pre_algebra/unit1.rs.html +++ b/src/ladderz/pre_algebra/unit1.rs.html @@ -1,4 +1,4 @@ -unit1.rs - source 1 +unit1.rs - source \ No newline at end of file 1 2 3 4 @@ -61,10 +61,90 @@ 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 +136use std::collections::HashSet; -// TODO: Implement negative integers -/// Generates a `HashSet` of factor pairs for a given positive integer `n`. +/// Finds all factor pairs for a given positive integer. +/// +/// # Challenge +/// +/// Write a program that finds all the factor pairs for a given number `n`. +/// +/// # Description +/// +/// Generates a `HashSet` of factor pairs for a given 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 @@ -113,15 +193,81 @@ factor_pairs } +/// Finds all factors of a given positive integer. +/// +/// # Challenge +/// +/// Write a program that finds all the factors of a given number. Assume that `n` is a positive integer greater than or equal to 1. +/// +/// # Description +/// +/// Generates a `HashSet` of factors for a given 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 +} + #[cfg(test)] mod tests { use super::*; #[test] - fn test_get_factor_pairs_1() { + 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); } }