feat: initial commit

This is the initial commit for the project.
Began work on pre-algebra unit 1.
This commit is contained in:
rzmk 2023-09-18 23:47:49 -04:00
commit 7b17b94c14
No known key found for this signature in database
10 changed files with 249 additions and 0 deletions

14
ladderz/.gitignore vendored Normal file
View file

@ -0,0 +1,14 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

16
ladderz/Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "ladderz"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[[bin]]
name = "ladderz"
src = "src/main.rs"
[lib]
name = "ladderz"
path = "src/lib.rs"
[dependencies]

4
ladderz/src/lib.rs Normal file
View file

@ -0,0 +1,4 @@
//! Non-production implementations of mathematical and technical concepts in Rust.
/// Various pre-algebra implementations including multiples (planned), factor pairs, etc.
pub mod pre_algebra;

View file

@ -0,0 +1,2 @@
/// Factors and multiples
pub mod unit1;

View file

@ -0,0 +1,63 @@
use std::collections::HashSet;
// TODO: Implement negative integers
/// 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`).
///
/// # 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
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_factor_pairs_1() {
let result: HashSet<(u32, u32)> = get_factor_pairs(1);
let expected: HashSet<(u32, u32)> = [(1, 1)].into();
assert_eq!(result, expected);
}
}