Are comprehensions over ranges optimized by the compiler

there is this exercism exercise:

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}.
[Pythagorean Triplet in Elixir on Exercism]

I wonder if I should optimize the ranges here:

for a <- 1..sum,
    b <- (a + 1)..sum,
    c = sum - a - b,
    c > b,
    triplet = [a, b, c],
    pythagorean?(triplet) do
  triplet
end

for example I could use a..div(sum/2). Or does the compiler (using the filter c > b) optimize this?
I understand, that everything after the filter will not be executed (if the filter is falsy), the question is if the stuff before gets optimized away.

It does not.

2 Likes

This is more in the domain of constraint programming packages like CLP(FD) in Prolog or python-constraint