Abhishek Kumar - 7 months ago 39

Scala Question

`Seq.fill(n)(math.pow(Random.nextFloat,2) + math.pow(Random.nextFloat,2)).filter(_<1).size.toFloat/n*4`

Basically this scala code checks number of times a random points comes out of first quadrant of a unit circle. For large values of

`n`

Answer

If you use an `Iterator`

no intermediate collection has to be created in memory.

```
Iterator.fill(n)(math.pow(Random.nextFloat,2) + math.pow(Random.nextFloat,2)).filter(_<1).size.toFloat/n*4
```

Source (Stackoverflow)