Implementing a few algorithms in Scala