Why is
for (
a <- 1 to 1000;
b <- 1 to 1000 - a;
c <- 1 to 1000 - a - b;
if (a * a + b * b == c * c && a + b + c == 1000)
) println((a, b, c, a * b * c))
266 ms
slower then:
for (a <- 1 to 1000)
for (b <- 1 to 1000 - a)
for (c <- 1 to 1000 - a - b)
if (a * a + b * b == c * c)
if (a + b + c == 1000)
println((a, b, c, a * b * c))
62 ms
If I understand correct this should be the same?
Solution after processing answers:
for (
a <- 1 to 1000;
b <- 1 to (1000 - a)
) {
val c = (1000 - a - b)
if (a * a + b * b == c * c)
println((a, b, c, a * b * c))
}
9 ms
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…