0
val SumABC = 1000
val Max = 468
val Min = 32

val p9 = for {
  a <- Max to 250 by -1
  b <- Min+(Max-a) to 249
  if a*a+b*b == (SumABC-a-b)*(SumABC-a-b)
} yield a*b*(SumABC-a-b)

Can I .take(1) here? (I tried to translate it to flatmap, filter, etc, but since I failed I guess it wouldn't be as readable anyway...)

  • 3
    If you want the loop to terminate after the first good element has been found, see http://stackoverflow.com/questions/13343531/how-to-yield-a-single-element-from-for-loop-in-scala – Malte Schwerhoff Nov 14 '12 at 09:54

1 Answers1

2

If I understood your cryptic questin, what you would like to do is the following

val p9 = (for {
  a <- Max to 250 by -1
  b <- Min+(Max-a) to 249
  if a*a+b*b == (SumABC-a-b)*(SumABC-a-b)
} yield a*b*(SumABC-a-b)).take(1)

Just add parenthesis before for and after yield to ensure the take method is called on the result of the for block

Edmondo
  • 19,559
  • 13
  • 62
  • 115