Converted MathHelper to Scala, implemented caching integer square root.