Published at Feb 08 2019
·
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}`

.

To run the tests, run the command `dotnet test`

from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip`

property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using `exercism submit PythagoreanTriplet.cs`

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

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.

```
// This file was auto-generated based on version 1.0.0 of the canonical data.
using System;
using Xunit;
public class PythagoreanTripletTest
{
[Fact]
public void Triplets_whose_sum_is_12()
{
Assert.Equal(new[]
{
(3, 4, 5)
}, PythagoreanTriplet.TripletsWithSum(12));
}
[Fact(Skip = "Remove to run test")]
public void Triplets_whose_sum_is_108()
{
Assert.Equal(new[]
{
(27, 36, 45)
}, PythagoreanTriplet.TripletsWithSum(108));
}
[Fact(Skip = "Remove to run test")]
public void Triplets_whose_sum_is_1000()
{
Assert.Equal(new[]
{
(200, 375, 425)
}, PythagoreanTriplet.TripletsWithSum(1000));
}
[Fact(Skip = "Remove to run test")]
public void No_matching_triplets_for_1001()
{
Assert.Equal(Array.Empty<(int, int, int)>(), PythagoreanTriplet.TripletsWithSum(1001));
}
[Fact(Skip = "Remove to run test")]
public void Returns_all_matching_triplets()
{
Assert.Equal(new[]
{
(9, 40, 41),
(15, 36, 39)
}, PythagoreanTriplet.TripletsWithSum(90));
}
[Fact(Skip = "Remove to run test")]
public void Several_matching_triplets()
{
Assert.Equal(new[]
{
(40, 399, 401),
(56, 390, 394),
(105, 360, 375),
(120, 350, 370),
(140, 336, 364),
(168, 315, 357),
(210, 280, 350),
(240, 252, 348)
}, PythagoreanTriplet.TripletsWithSum(840));
}
[Fact(Skip = "Remove to run test")]
public void Triplets_for_large_number()
{
Assert.Equal(new[]
{
(1200, 14375, 14425),
(1875, 14000, 14125),
(5000, 12000, 13000),
(6000, 11250, 12750),
(7500, 10000, 12500)
}, PythagoreanTriplet.TripletsWithSum(30000));
}
}
```

```
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
public static class PythagoreanTriplet
{
/// <summary>
/// a less b less c therefore a less sum / 3; b less (sum - 1) / 2
/// </summary>
public static IEnumerable<(int a, int b, int c)> TripletsWithSum(int sum)
{
if (sum < 6)
{
throw new ArgumentException("sum must be more than 5");
}
return Enumerable.Range(1, sum / 3 - 1)
.SelectMany(a => Enumerable.Range(a + 1, ((sum - 1) / 2) - (a + 1)).Select(b => (a: a, b: b, c: sum - a - b)))
.Where(x => x.a * x.a + x.b * x.b == x.c * x.c);
}
}
```

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?

## Community comments