I'm working on project euler 9 and I've solved it various ways. I'm using these problems to learn functional programing techniques. I'm now attempting to mimic my friend Chris recursive elixir solution:
defmodule PythagoreanTriplet do
def find_triplet(a \\ 1, b \\ 2) do
c = :math.pow(a, 2) + :math.pow(b, 2) |> :math.sqrt
cond do
a + b + c == 1000.0 -> IO.puts "#{a} * #{b} * #{c} = #{a * b * c}"
b < 500 -> find_triplet(a, b + 1)
a < 500 -> find_triplet(a + 1, a + 2)
true -> IO.puts "I don't know"
end
end
end
PythagoreanTriplet.find_triplet
Here is my attempt in Mathematica:
Clear[findTriple]
findTriple[a0_, b0_] := findTriple[a0, b0] =
Module[
{a = a0, b = b0, c = Sqrt[a0^2 + b0^2]},
Which[a + b + c == 1000, Print[a*b*c],
b < 500, findTriple[a, b + 1],
a < 500, findTriple[a + 1, a + 2]
]
]
findTriple[1, 2]
I'm getting $RecursionLimit: Recursion depth of 1024 exceeded during evaluation
errors. I've read through related posts and I'm over my head. Thank you.
FindInstance[a^2 + b^2 == c^2 ∧ a + b + c == 1000 ∧ a < b < c ∧ {a, b, c} ∈ Integers ∧ {a, b, c} > 0, {a, b, c}, 1]
$\endgroup$