Published at Dec 19 2018
·
0 comments

Instructions

Test suite

Solution

A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for which,

```
a**2 + b**2 = c**2
```

and such that,

```
a < b < c
```

For example,

```
3**2 + 4**2 = 9 + 16 = 25 = 5**2.
```

Given an input integer N, find all Pythagorean triplets for which `a + b + c = N`

.

For example, with N = 1000, there is exactly one Pythagorean triplet for which `a + b + c = 1000`

: `{200, 375, 425}`

.

Refer to the exercism help page for Rust installation and learning resources.

Execute the tests with:

```
$ cargo test
```

All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests`

directory
and remove the `#[ignore]`

flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]`

flag above it.
Continue, until you pass every test.

If you wish to run all tests without editing the tests source file, use:

```
$ cargo test -- --ignored
```

To run a specific test, for example `some_test`

, you can use:

```
$ cargo test some_test
```

If the specific test is ignored use:

```
$ cargo test some_test -- --ignored
```

To learn more about Rust tests refer to the online test documentation

Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.

The exercism/rust repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!

If you want to know more about Exercism, take a look at the contribution guide.

Problem 9 at Project Euler http://projecteuler.net/problem=9

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
use pythagorean_triplet::find;
use std::{collections::HashSet, iter::FromIterator};
fn process_tripletswithsum_case(sum: u32, expected: &[[u32; 3]]) {
let triplets = find(sum);
if !expected.is_empty() {
let expected = HashSet::from_iter(expected.iter().cloned());
assert_eq!(expected, triplets);
} else {
assert!(triplets.is_empty());
}
}
#[test]
fn test_triplets_whose_sum_is_12() {
process_tripletswithsum_case(12, &[[3, 4, 5]]);
}
#[test]
#[ignore]
fn test_triplets_whose_sum_is_108() {
process_tripletswithsum_case(108, &[[27, 36, 45]]);
}
#[test]
#[ignore]
fn test_triplets_whose_sum_is_1000() {
process_tripletswithsum_case(1000, &[[200, 375, 425]]);
}
#[test]
#[ignore]
fn test_no_matching_triplets_for_1001() {
process_tripletswithsum_case(1001, &[]);
}
#[test]
#[ignore]
fn test_returns_all_matching_triplets() {
process_tripletswithsum_case(90, &[[9, 40, 41], [15, 36, 39]]);
}
#[test]
#[ignore]
fn test_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 test_triplets_for_large_number() {
process_tripletswithsum_case(
30000,
&[
[1200, 14375, 14425],
[1875, 14000, 14125],
[5000, 12000, 13000],
[6000, 11250, 12750],
[7500, 10000, 12500],
],
);
}
```

```
use std::collections::HashSet;
pub fn find(sum: u32) -> HashSet<[u32; 3]> {
let mut ans: HashSet<[u32; 3]> = HashSet::new();
'outer: for a in 1..sum {
for b in 1..sum {
if (a + b) >= sum {
continue 'outer;
}
let c = sum - (a + b);
if ((a < b) && (b < c)) && (a + b + c) == sum {
if a.pow(2) + b.pow(2) == c.pow(2) {
ans.insert([a, b, c]);
}
}
}
}
ans
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,387 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments