Skip to content

Commit

Permalink
rust/perfect-numbers: 1st iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Sep 23, 2023
1 parent b2042b7 commit e8c765b
Show file tree
Hide file tree
Showing 12 changed files with 23,733 additions and 22,373 deletions.
1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"./luhn-from",
"./luhn-trait",
"./pangram",
"./perfect-numbers",
"./raindrops",
"./reverse-string",
"./roman-numerals",
Expand Down
1 change: 1 addition & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@
- [bob](./bob/README.md)
- [all-your-base](./all-your-base/README.md)
- [high-scores](./high-scores/README.md)
- [perfect-numbers](./perfect-numbers/README.md)
45,286 changes: 22,929 additions & 22,357 deletions rust/index.lsif

Large diffs are not rendered by default.

Binary file modified rust/index.scip
Binary file not shown.
2 changes: 0 additions & 2 deletions rust/perfect-numbers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
edition = "2021"
name = "perfect_numbers"
version = "1.1.0"

[dependencies]
7 changes: 6 additions & 1 deletion rust/perfect-numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ Implement a way to determine whether a given number is **perfect**. Depending on

### Based on

Taken from Chapter 2 of Functional Thinking by Neal Ford. - http://shop.oreilly.com/product/0636920029687.do
Taken from Chapter 2 of Functional Thinking by Neal Ford. - http://shop.oreilly.com/product/0636920029687.do

### My Solution

- [my solution](./src/lib.rs)
- [run-tests](./run-tests-rust.txt)
128 changes: 128 additions & 0 deletions rust/perfect-numbers/coverage-annotations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
1| 33|#[derive(Debug, PartialEq, Eq)]
^0 ^0
------------------
| Unexecuted instantiation: <perfect_numbers::Classification as core::fmt::Debug>::fmt
------------------
| Unexecuted instantiation: <perfect_numbers::Classification as core::fmt::Debug>::fmt
------------------
| Unexecuted instantiation: <perfect_numbers::Classification as core::cmp::PartialEq>::eq
------------------
| <perfect_numbers::Classification as core::cmp::PartialEq>::eq:
| 1| 33|#[derive(Debug, PartialEq, Eq)]
------------------
| Unexecuted instantiation: <perfect_numbers::Classification as core::cmp::PartialEq>::eq
------------------
2| |pub enum Classification {
3| | Abundant,
4| | Perfect,
5| | Deficient,
6| |}
7| |
8| |/// Determine if a number is perfect, abundant or deficient based on Nicomachus' (60 - 120 CE) classification scheme for positive integers.
9| |///
10| |/// Example - Invalid Input
11| |/// ```rust
12| |/// use perfect_numbers::*;
13| |///
14| |/// let want: Option<Classification> = None;
15| |///
16| |/// match classify(0) {
17| |/// Some(got) => { assert!(false); }
18| |/// None => { assert!(true) },
19| |/// };
20| |/// ```
21| |///
22| |/// Example - Perfect
23| |/// ```rust
24| |/// use perfect_numbers::*;
25| |///
26| |/// let want: Classification = Classification::Perfect;
27| |///
28| |/// match classify(6) {
29| |/// Some(got) => { assert_eq!(got, want); }
30| |/// None => {},
31| |/// };
32| |/// ```
33| |///
34| |/// Example - Abundant
35| |/// ```rust
36| |/// use perfect_numbers::*;
37| |///
38| |/// let want: Classification = Classification::Abundant;
39| |///
40| |/// match classify(12) {
41| |/// Some(got) => { assert_eq!(got, want); }
42| |/// None => {},
43| |/// };
44| |/// ```
45| |///
46| |/// Example - Deficient
47| |/// ```rust
48| |/// use perfect_numbers::*;
49| |///
50| |/// let want: Classification = Classification::Deficient;
51| |///
52| |/// match classify(8) {
53| |/// Some(got) => { assert_eq!(got, want); }
54| |/// None => {},
55| |/// };
56| |/// ```
57| 36|pub fn classify(number: u64) -> Option<Classification> {
58| 36| if number == 0 {
59| 3| return None;
60| 33| }
61| 33|
62| 33| let mut factors: Vec<u64> = Vec::new();
63| 33|
64| 33| let mut factor: u64 = 1;
65| |
66| 301M| while factor < number {
67| 301M| if number % factor == 0 {
68| 351| factors.push(factor);
69| 301M| }
70| |
71| 301M| factor += 1;
72| | }
73| |
74| 33| let aliquot_sum: u64 = factors.iter().sum();
75| 33|
76| 33| Some(match aliquot_sum {
77| 33| sum if sum > number => Classification::Abundant,
^9 ^9
78| 24| sum if sum < number => Classification::Deficient,
^15 ^15
79| 9| _ => Classification::Perfect,
80| | })
81| 36|}
------------------
| Unexecuted instantiation: perfect_numbers::classify
------------------
| perfect_numbers::classify:
| 57| 36|pub fn classify(number: u64) -> Option<Classification> {
| 58| 36| if number == 0 {
| 59| 3| return None;
| 60| 33| }
| 61| 33|
| 62| 33| let mut factors: Vec<u64> = Vec::new();
| 63| 33|
| 64| 33| let mut factor: u64 = 1;
| 65| |
| 66| 301M| while factor < number {
| 67| 301M| if number % factor == 0 {
| 68| 351| factors.push(factor);
| 69| 301M| }
| 70| |
| 71| 301M| factor += 1;
| 72| | }
| 73| |
| 74| 33| let aliquot_sum: u64 = factors.iter().sum();
| 75| 33|
| 76| 33| Some(match aliquot_sum {
| 77| 33| sum if sum > number => Classification::Abundant,
| ^9 ^9
| 78| 24| sum if sum < number => Classification::Deficient,
| ^15 ^15
| 79| 9| _ => Classification::Perfect,
| 80| | })
| 81| 36|}
------------------
8 changes: 8 additions & 0 deletions rust/perfect-numbers/coverage-missing-lines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Running: cargo llvm-cov --no-clean --all-features --show-missing-lines

Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/home/vpayno/git_vpayno/exercism-workspace/rust/perfect-numbers/src/lib.rs 20 2 90.00% 3 1 66.67% 22 1 95.45% 0 0 -
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL 20 2 90.00% 3 1 66.67% 22 1 95.45% 0 0 -

43 changes: 43 additions & 0 deletions rust/perfect-numbers/report.lcov
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
SF:/home/vpayno/git_vpayno/exercism-workspace/rust/perfect-numbers/src/lib.rs
FN:1,_RNvXs0_CshU4NEG0KGoF_15perfect_numbersNtB5_14ClassificationNtNtCs8zepCCxJ8Q4_4core3cmp9PartialEq2eqB5_
FN:1,_RNvXCshU4NEG0KGoF_15perfect_numbersNtB2_14ClassificationNtNtCs8zepCCxJ8Q4_4core3fmt5Debug3fmt
FN:57,_RNvCshU4NEG0KGoF_15perfect_numbers8classify
FN:1,_RNvXs0_Cscb4KXbkaF77_15perfect_numbersNtB5_14ClassificationNtNtCs8zepCCxJ8Q4_4core3cmp9PartialEq2eqCsfVn1n8yW6ma_15perfect_numbers
FN:1,_RNvXs0_Cscb4KXbkaF77_15perfect_numbersNtB5_14ClassificationNtNtCs8zepCCxJ8Q4_4core3cmp9PartialEq2eqB5_
FN:57,_RNvCscb4KXbkaF77_15perfect_numbers8classify
FN:1,_RNvXCscb4KXbkaF77_15perfect_numbersNtB2_14ClassificationNtNtCs8zepCCxJ8Q4_4core3fmt5Debug3fmt
FNDA:0,_RNvXs0_CshU4NEG0KGoF_15perfect_numbersNtB5_14ClassificationNtNtCs8zepCCxJ8Q4_4core3cmp9PartialEq2eqB5_
FNDA:0,_RNvXCshU4NEG0KGoF_15perfect_numbersNtB2_14ClassificationNtNtCs8zepCCxJ8Q4_4core3fmt5Debug3fmt
FNDA:0,_RNvCshU4NEG0KGoF_15perfect_numbers8classify
FNDA:11,_RNvXs0_Cscb4KXbkaF77_15perfect_numbersNtB5_14ClassificationNtNtCs8zepCCxJ8Q4_4core3cmp9PartialEq2eqCsfVn1n8yW6ma_15perfect_numbers
FNDA:0,_RNvXs0_Cscb4KXbkaF77_15perfect_numbersNtB5_14ClassificationNtNtCs8zepCCxJ8Q4_4core3cmp9PartialEq2eqB5_
FNDA:12,_RNvCscb4KXbkaF77_15perfect_numbers8classify
FNDA:0,_RNvXCscb4KXbkaF77_15perfect_numbersNtB2_14ClassificationNtNtCs8zepCCxJ8Q4_4core3fmt5Debug3fmt
FNF:3
FNH:2
DA:1,11
DA:57,12
DA:58,12
DA:59,1
DA:60,11
DA:61,11
DA:62,11
DA:63,11
DA:64,11
DA:66,100651123
DA:67,100651112
DA:68,117
DA:69,100650995
DA:71,100651112
DA:74,11
DA:75,11
DA:76,11
DA:77,11
DA:78,8
DA:79,3
DA:81,12
BRF:0
BRH:0
LF:22
LH:21
end_of_record
Loading

0 comments on commit e8c765b

Please sign in to comment.