🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # freddie2025's solution

## to Pythagorean Triplet in the C# Track

Published at Apr 16 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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}`.

## Running the tests

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`

## Further information

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.

## Source

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

### PythagoreanTripletTest.cs

``````// 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.Linq;
using System.Collections.Generic;

public static class PythagoreanTriplet
{
public static IEnumerable<(int a, int b, int c)> TripletsWithSum(int sum)
{
int b = 0;

for(int i = 1; (i + b) + (Math.Sqrt(i*i + b*b)) <= sum; i++)
{
b = i+1;

for(int j = i+1; (i + j) + (Math.Sqrt(i*i + j*j)) <= sum; j++)
{
if((i + j) + (Math.Sqrt(i*i + j*j)) == sum)
{
yield return (a: i, b: j, c: (int)Math.Sqrt(i*i + j*j));
}
}
}
}
}``````