Avatar of JaeHyoLee

JaeHyoLee's solution

to Pythagorean Triplet in the Lua Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

Running the tests

To run the tests, run the command busted from within the exercise directory.

Further information

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.

Source

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

Submitting Incomplete Solutions

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

pythagorean-triplet_spec.lua

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
}

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of ryanplusplus

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.

What can you learn from this solution?

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?