Skip to content

Commit

Permalink
pythagorean triplet
Browse files Browse the repository at this point in the history
  • Loading branch information
vhgcuong committed Oct 31, 2023
1 parent 0e9f4bf commit e0870dc
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub mod spiral_matrix;
pub mod perfect_numbers;
pub mod nucleotide_count;
pub mod rna_transcription;
pub mod pythagorean_triplet;
pub mod largest_series_product;

// Hard
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use exercism::isbn_verifier;
use exercism::pythagorean_triplet;
fn main() {
isbn_verifier::is_valid_isbn("3-598-21507-X");
pythagorean_triplet::find(1001);
}
23 changes: 23 additions & 0 deletions src/pythagorean_triplet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::collections::HashSet;

pub fn find(sum: u32) -> HashSet<[u32; 3]> {
let mut result: HashSet<[u32; 3]> = HashSet::new();
for a in 1..=sum / 2 {
for b in a..sum / 2 {
if a + b > sum {
break;
}

let c = sum - b - a;
if c < a || c < b {
continue;
}

if a * a + b * b == c * c {
result.insert([a, b, c]);
}
}
}

result
}
1 change: 1 addition & 0 deletions tests/libtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ mod allergies;
mod nucleotide_count;
mod hamming;
mod isbn_verifier;
mod pythagorean_triplet;
66 changes: 66 additions & 0 deletions tests/pythagorean_triplet/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use exercism::pythagorean_triplet::find;
use std::collections::HashSet;
fn process_tripletswithsum_case(sum: u32, expected: &[[u32; 3]]) {
let triplets = find(sum);
if !expected.is_empty() {
let expected: HashSet<_> = expected.iter().cloned().collect();
assert_eq!(expected, triplets);
} else {
assert!(triplets.is_empty());
}
}
#[test]
fn triplets_whose_sum_is_12() {
process_tripletswithsum_case(12, &[[3, 4, 5]]);
}
#[test]
#[ignore]
fn triplets_whose_sum_is_108() {
process_tripletswithsum_case(108, &[[27, 36, 45]]);
}
#[test]
#[ignore]
fn triplets_whose_sum_is_1000() {
process_tripletswithsum_case(1000, &[[200, 375, 425]]);
}
#[test]
#[ignore]
fn no_matching_triplets_for_1001() {
process_tripletswithsum_case(1001, &[]);
}
#[test]
#[ignore]
fn returns_all_matching_triplets() {
process_tripletswithsum_case(90, &[[9, 40, 41], [15, 36, 39]]);
}
#[test]
#[ignore]
fn several_matching_triplets() {
process_tripletswithsum_case(
840,
&[
[40, 399, 401],
[56, 390, 394],
[105, 360, 375],
[120, 350, 370],
[140, 336, 364],
[168, 315, 357],
[210, 280, 350],
[240, 252, 348],
],
);
}
#[test]
#[ignore]
fn triplets_for_large_number() {
process_tripletswithsum_case(
30_000,
&[
[1200, 14_375, 14_425],
[1875, 14_000, 14_125],
[5000, 12_000, 13_000],
[6000, 11_250, 12_750],
[7500, 10_000, 12_500],
],
);
}

0 comments on commit e0870dc

Please sign in to comment.