Avatar of JBaack

JBaack's solution

to Pythagorean Triplet in the Lua Track

Published at Oct 01 2019 · 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

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 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 triplets_with_sum = 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('triplets_with_sum', function()
    it('finds triplets whose sum is 12', function()
      assert.same(
        { { 3, 4, 5 } },
        sort(triplets_with_sum(12))
      )
    end)

    it('finds triplets whose sum is 108', function()
      assert.same(
        { { 27, 36, 45 } },
        sort(triplets_with_sum(108))
      )
    end)

    it('finds triplets whose sum is 1000', function()
      assert.same(
        { { 200, 375, 425 } },
        sort(triplets_with_sum(1000))
      )
    end)

    it('finds no triplets whose sum is 1001', function()
      assert.same({}, triplets_with_sum(1001))
    end)

    it('finds triplets whose sum is 90', function()
      assert.same(
        {
          { 9, 40, 41 },
          { 15, 36, 39 },
        },
        sort(triplets_with_sum(90))
      )
    end)

    it('finds triplets whose sum is 840', function()
      assert.same(
        {
          { 40, 399, 401 },
          { 56, 390, 394 },
          { 105, 360, 375 },
          { 120, 350, 370 },
          { 140, 336, 364 },
          { 168, 315, 357 },
          { 210, 280, 350 },
          { 240, 252, 348 },
        },
        sort(triplets_with_sum(840))
      )
    end)

    it('finds triplets whose sum is a large number (30000)', function()
      assert.same(
        {
          { 1200, 14375, 14425 },
          { 1875, 14000, 14125 },
          { 5000, 12000, 13000 },
          { 6000, 11250, 12750 },
          { 7500, 10000, 12500 },
        },
        sort(triplets_with_sum(30000))
      )
    end)
  end)
end)
return function(sum_of_sides)

   local solutions = { }
   local a, b, c
   local first_length = (sum_of_sides - sum_of_sides % 2) / 2
   local last_length = (sum_of_sides - sum_of_sides % 2.5) / 2.5

   for c = first_length, last_length, -1 do
      for b = c - 1, (c - c % 2) / 2 + 1, -1 do
         a = sum_of_sides - (c + b)
         if (a * a + b * b == c * c) and (a < b) then
            table.insert(solutions, {a, b, c})
         end
      end
   end

   return solutions
end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of JBaack
JBaack
Solution Author
commented 286 days ago

By calculating the first and last lengths outside the for loop and doing (a * a), (b * b), ... instead of a ^ 2 reduced the run time from almost 4 seconds down to 0.8 seconds

(edited 286 days ago)

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?