Abhishek Kumar - 1 month ago 7

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
```