ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Jul 13 2018
·
1 comment

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

For example,

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

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product a * b * c.

To run the tests, run the command `busted`

from within the exercise directory.

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua 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.

```
local pythagorean = require('pythagorean-triplet')
describe('pythagorean-triplet', function()
local function sort(triplets)
table.sort(triplets, function(a, b) return a[1] < b[1] end)
return triplets
end
describe('is_triplet', function()
it('should identify triplets', function()
assert.is_true(pythagorean.is_triplet(3, 4, 5))
assert.is_true(pythagorean.is_triplet(5, 12, 13))
end)
it('should identify non-triplets', function()
assert.is_false(pythagorean.is_triplet(3, 4, 6))
assert.is_false(pythagorean.is_triplet(5, 6, 17))
end)
end)
describe('triplets_with', function()
it('should generate all triplets with a specified maximum factor', function()
assert.same(
{ { 3, 4, 5 }, { 5, 12, 13 }, { 6, 8, 10 }, { 8, 15, 17}, { 9, 12, 15 } },
sort(pythagorean.triplets_with{ max_factor = 17 })
)
end)
it('should generate all triplets with a specified minimum and maximum factor', function()
assert.same(
{ { 6, 8, 10 }, { 9, 12, 15 } },
sort(pythagorean.triplets_with{ min_factor = 6, max_factor = 15 })
)
end)
it('should generate all triplets with a specified maximum factor and a specified sum', function()
assert.same(
{ { 18, 80, 82 }, { 30, 72, 78 }, { 45, 60, 75 } },
sort(pythagorean.triplets_with{ sum = 180, max_factor = 100 })
)
end)
end)
end)
```

```
local function is_triplet(a, b, c)
return a^2 + b^2 == c^2
end
local function is_natural_number(n)
return n==math.floor(n) and n > 0
end
local function triplets_with(constraints)
local min = constraints.min_factor or 3
local max = constraints.max_factor or assert(fail,"max_factor is mandatory")
local result = {}
for a = min, max do
for b = a + 1, max do
local c = math.sqrt(a^2 + b^2)
if (is_natural_number(c) and c <= max and (a+b+c) == (constraints.sum or a+b+c)) then table.insert(result, {a, b, c}) end
end
end
return result
end
return {
is_triplet = is_triplet,
triplets_with = triplets_with
}
```

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,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

Cool, I like how you avoid iterating through c. I wonder how the performance of this compares to mine. I think for large max it will be significantly faster.