Solved the 7th problem in Scala

There is an error though. Because I have defined the function
recursively, but it's dealing with mutation, this causes an overflow
error. Calling twice will fix it, but ideally, it shouldn't matter how
large or how many calls are made. I need to find a way to make it tail
form recursive, or completely just use imperative structures.