Skip to content

Problem 9: Special Pythagorean triplet

Link to the problem: https://projecteuler.net/problem=9.

Problem 9

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 \(abc\).

A simple solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
(defwire main-wire
  1000 = .s
  0 >= .a >= .b >= .c

  .s (Math.Subtract 3) (Math.Divide 3) = .max_a
  (ForRange
   3 .max_a
   (-> > .a
       .a (Math.Add 1) >= .min_b
       .s (Math.Subtract 1) (Math.Subtract .a) (Math.Divide 2) >= .max_b
       (ForRange
        .min_b .max_b
        (-> > .b
            .s (Math.Subtract .a) (Math.Subtract .b) > .c
            .a (Math.Multiply .a) >= .sqr_a
            .b (Math.Multiply .b) >= .sqr_b
            .c (Math.Multiply .c) >= .sqr_c
            (When (-> .sqr_a (Math.Add .sqr_b) (Is .sqr_c))
                  (-> .a (Math.Multiply .b) (Math.Multiply .c)

                      (Assert.Is 31875000)
                      (Log "Answer"))))))))

(defmesh root)
(schedule root main-wire)
(run root)
[info] [2023-02-24 14:01:42.420] [T-2000] [logging.cpp::62] [main-wire] Answer: 31875000

More details on the Project Euler's website: https://projecteuler.net/overview=009.