diff --git a/ladderz/all.html b/ladderz/all.html index 03c9b05..bd5f656 100644 --- a/ladderz/all.html +++ b/ladderz/all.html @@ -1 +1 @@ -List of all items in this crate

List of all items

Functions

\ No newline at end of file +List of all items in this crate

List of all items

Functions

\ No newline at end of file diff --git a/ladderz/pre_algebra/fn.get_prime_factorization.html b/ladderz/pre_algebra/fn.get_prime_factorization.html new file mode 100644 index 0000000..8d22e9b --- /dev/null +++ b/ladderz/pre_algebra/fn.get_prime_factorization.html @@ -0,0 +1,11 @@ +get_prime_factorization in ladderz::pre_algebra - Rust
pub fn get_prime_factorization(n: u32) -> HashMap<u32, u32>
Expand description

Write a program that determines the prime factorization of a positive integer n.

+

For example the prime factorization of 12 is 22 * 31, and the output is a HashMap of the form +[(2, 2), (3, 1)] where the first element of each tuple is the prime factor and the second element is the exponent.

+

Examples

+
use ladderz::pre_algebra::get_prime_factorization;
+use std::collections::HashMap;
+
+let result: HashMap<u32, u32> = get_prime_factorization(12);
+let expected: HashMap<u32, u32> = [(2, 2), (3, 1)].into();
+assert_eq!(result, expected);
+
\ No newline at end of file diff --git a/ladderz/pre_algebra/fn.get_primes_in_range.html b/ladderz/pre_algebra/fn.get_primes_in_range.html new file mode 100644 index 0000000..1e05912 --- /dev/null +++ b/ladderz/pre_algebra/fn.get_primes_in_range.html @@ -0,0 +1,11 @@ +get_primes_in_range in ladderz::pre_algebra - Rust
pub fn get_primes_in_range(start: u32, n: u32) -> HashSet<u32>
Expand description

Write a program that finds all prime numbers of a positive integer n in the range [start, n].

+

A prime number is a positive integer greater than 1 that is +not evenly divisible by any positive integer other than 1 and itself.

+

Examples

+
use ladderz::pre_algebra::get_primes_in_range;
+use std::collections::HashSet;
+
+let result: HashSet<u32> = get_primes_in_range(2, 10);
+let expected: HashSet<u32> = [2, 3, 5, 7].into();
+assert_eq!(result, expected);
+
\ No newline at end of file diff --git a/ladderz/pre_algebra/index.html b/ladderz/pre_algebra/index.html index b3dc98f..c88fdc8 100644 --- a/ladderz/pre_algebra/index.html +++ b/ladderz/pre_algebra/index.html @@ -5,4 +5,4 @@ let x: u32 = 10; println!("The factors of {x} are {:?}.", get_factors(x));
The factors of 10 are {1, 5, 2, 10}.
-

Functions

\ No newline at end of file +

Functions

\ No newline at end of file diff --git a/ladderz/pre_algebra/sidebar-items.js b/ladderz/pre_algebra/sidebar-items.js index 387a36a..afc6a5b 100644 --- a/ladderz/pre_algebra/sidebar-items.js +++ b/ladderz/pre_algebra/sidebar-items.js @@ -1 +1 @@ -window.SIDEBAR_ITEMS = {"fn":["get_factor_pairs","get_factors","get_multiples_in_range","is_composite","is_factor","is_multiple","is_prime"]}; \ No newline at end of file +window.SIDEBAR_ITEMS = {"fn":["get_factor_pairs","get_factors","get_multiples_in_range","get_prime_factorization","get_primes_in_range","is_composite","is_factor","is_multiple","is_prime"]}; \ No newline at end of file diff --git a/ladderz/pre_algebra/unit1/fn.get_prime_factorization.html b/ladderz/pre_algebra/unit1/fn.get_prime_factorization.html new file mode 100644 index 0000000..9aafba1 --- /dev/null +++ b/ladderz/pre_algebra/unit1/fn.get_prime_factorization.html @@ -0,0 +1,11 @@ + + + + + Redirection + + +

Redirecting to ../../../ladderz/pre_algebra/fn.get_prime_factorization.html...

+ + + \ No newline at end of file diff --git a/ladderz/pre_algebra/unit1/fn.get_primes_in_range.html b/ladderz/pre_algebra/unit1/fn.get_primes_in_range.html new file mode 100644 index 0000000..d727259 --- /dev/null +++ b/ladderz/pre_algebra/unit1/fn.get_primes_in_range.html @@ -0,0 +1,11 @@ + + + + + Redirection + + +

Redirecting to ../../../ladderz/pre_algebra/fn.get_primes_in_range.html...

+ + + \ No newline at end of file diff --git a/search-index.js b/search-index.js index 0cb6ee0..f458030 100644 --- a/search-index.js +++ b/search-index.js @@ -1,5 +1,5 @@ var searchIndex = JSON.parse('{\ -"ladderz":{"doc":"ladderz","t":"AFFFFFFF","n":["pre_algebra","get_factor_pairs","get_factors","get_multiples_in_range","is_composite","is_factor","is_multiple","is_prime"],"q":[[0,"ladderz"],[1,"ladderz::pre_algebra"]],"d":["Various pre-algebra implementations including factor …","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 n is a composite number.","Checks if a positive integer x is a factor of another …","Checks if a positive integer x is a multiple of another …","Checks if a positive integer n is a prime number."],"i":[0,0,0,0,0,0,0,0],"f":[0,[1,2],[1,[[2,[1]]]],[[1,1],[[2,[1]]]],[1,3],[[1,1],3],[[1,1],3],[1,3]],"c":[],"p":[[15,"u32"],[3,"HashSet"],[15,"bool"]]}\ +"ladderz":{"doc":"ladderz","t":"AFFFFFFFFF","n":["pre_algebra","get_factor_pairs","get_factors","get_multiples_in_range","get_prime_factorization","get_primes_in_range","is_composite","is_factor","is_multiple","is_prime"],"q":[[0,"ladderz"],[1,"ladderz::pre_algebra"]],"d":["Various pre-algebra implementations including factor …","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 …","Write a program that determines the prime factorization of …","Write a program that finds all prime numbers of a positive …","Checks if a positive integer n is a composite number.","Checks if a positive integer x is a factor of another …","Checks if a positive integer x is a multiple of another …","Checks if a positive integer n is a prime number."],"i":[0,0,0,0,0,0,0,0,0,0],"f":[0,[1,2],[1,[[2,[1]]]],[[1,1],[[2,[1]]]],[1,[[3,[1,1]]]],[[1,1],[[2,[1]]]],[1,4],[[1,1],4],[[1,1],4],[1,4]],"c":[],"p":[[15,"u32"],[3,"HashSet"],[3,"HashMap"],[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 dd3401b..77f00f0 100644 --- a/src/ladderz/pre_algebra/unit1.rs.html +++ b/src/ladderz/pre_algebra/unit1.rs.html @@ -276,7 +276,112 @@ 276 277 278 -
use std::collections::HashSet;
+279
+280
+281
+282
+283
+284
+285
+286
+287
+288
+289
+290
+291
+292
+293
+294
+295
+296
+297
+298
+299
+300
+301
+302
+303
+304
+305
+306
+307
+308
+309
+310
+311
+312
+313
+314
+315
+316
+317
+318
+319
+320
+321
+322
+323
+324
+325
+326
+327
+328
+329
+330
+331
+332
+333
+334
+335
+336
+337
+338
+339
+340
+341
+342
+343
+344
+345
+346
+347
+348
+349
+350
+351
+352
+353
+354
+355
+356
+357
+358
+359
+360
+361
+362
+363
+364
+365
+366
+367
+368
+369
+370
+371
+372
+373
+374
+375
+376
+377
+378
+379
+380
+381
+382
+383
+
use std::collections::{HashMap, HashSet};
 
 /// Finds all factor pairs for a positive integer `n`.
 ///
@@ -470,6 +575,68 @@
     false
 }
 
+/// Write a program that finds all prime numbers of a positive integer `n` in the range [start, n].
+///
+/// A prime number is a positive integer greater than 1 that is
+/// not evenly divisible by any positive integer other than 1 and itself.
+///
+/// # Examples
+///
+/// ```rust
+/// use ladderz::pre_algebra::get_primes_in_range;
+/// use std::collections::HashSet;
+///
+/// let result: HashSet<u32> = get_primes_in_range(2, 10);
+/// let expected: HashSet<u32> = [2, 3, 5, 7].into();
+/// assert_eq!(result, expected);
+/// ```
+pub fn get_primes_in_range(start: u32, n: u32) -> HashSet<u32> {
+    let mut primes: HashSet<u32> = HashSet::new();
+
+    for num in start..n + 1 {
+        if is_prime(num) {
+            primes.insert(num);
+        }
+    }
+    primes
+}
+
+/// Write a program that determines the prime factorization of a positive integer `n`.
+///
+/// For example the prime factorization of 12 is 2<sup>2</sup> * 3<sup>1</sup>, and the output is a HashMap of the form
+/// `[(2, 2), (3, 1)]` where the first element of each tuple is the prime factor and the second element is the exponent.
+///
+/// # Examples
+///
+/// ```rust
+/// use ladderz::pre_algebra::get_prime_factorization;
+/// use std::collections::HashMap;
+///
+/// let result: HashMap<u32, u32> = get_prime_factorization(12);
+/// let expected: HashMap<u32, u32> = [(2, 2), (3, 1)].into();
+/// assert_eq!(result, expected);
+/// ```
+pub fn get_prime_factorization(n: u32) -> HashMap<u32, u32> {
+    if n == 1 {
+        return HashMap::new();
+    }
+
+    let mut prime_factors: HashMap<u32, u32> = HashMap::new();
+    let mut current_num: u32 = n;
+    let primes_of_n: HashSet<u32> = get_primes_in_range(2, n);
+
+    for prime in primes_of_n {
+        while current_num % prime == 0 {
+            prime_factors
+                .entry(prime)
+                .and_modify(|e| *e += 1)
+                .or_insert(1);
+            current_num /= prime;
+        }
+    }
+    prime_factors
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -553,5 +720,48 @@
         assert!(is_composite(27));
         assert!(is_composite(51));
     }
+
+    #[test]
+    fn test_get_primes_in_range() {
+        let result: HashSet<u32> = get_primes_in_range(2, 10);
+        let expected: HashSet<u32> = [2, 3, 5, 7].into();
+        assert_eq!(result, expected);
+
+        let result_2: HashSet<u32> = get_primes_in_range(1, 10);
+        let expected_2: HashSet<u32> = [2, 3, 5, 7].into();
+        assert_eq!(result_2, expected_2);
+
+        let result_3: HashSet<u32> = get_primes_in_range(1, 1);
+        let expected_3: HashSet<u32> = [].into();
+        assert_eq!(result_3, expected_3);
+
+        let result_4: HashSet<u32> = get_primes_in_range(1, 50);
+        let expected_4: HashSet<u32> =
+            [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47].into();
+        assert_eq!(result_4, expected_4);
+    }
+
+    #[test]
+    fn test_get_prime_factorization() {
+        let result: HashMap<u32, u32> = get_prime_factorization(1);
+        let expected: HashMap<u32, u32> = HashMap::new();
+        assert_eq!(result, expected);
+
+        let result_2: HashMap<u32, u32> = get_prime_factorization(12);
+        let expected_2: HashMap<u32, u32> = [(2, 2), (3, 1)].into();
+        assert_eq!(result_2, expected_2);
+
+        let result_3: HashMap<u32, u32> = get_prime_factorization(16);
+        let expected_3: HashMap<u32, u32> = [(2, 4)].into();
+        assert_eq!(result_3, expected_3);
+
+        let result_4: HashMap<u32, u32> = get_prime_factorization(27);
+        let expected_4: HashMap<u32, u32> = [(3, 3)].into();
+        assert_eq!(result_4, expected_4);
+
+        let result_5: HashMap<u32, u32> = get_prime_factorization(51);
+        let expected_5: HashMap<u32, u32> = [(3, 1), (17, 1)].into();
+        assert_eq!(result_5, expected_5);
+    }
 }
 
\ No newline at end of file