Saturday, April 5, 2014

Scala Projects

So a Josh Branchaud (a friend of mine) and I have currently started working on learning new languages through a series of small scale problems. We stumbled upon this github repo that has a lot of both small and large problem ideas that this guy wrote solutions to in python for his own learning. We have decided to work through the same problems he outlines but in the languages we want to learn: I have chosen to write the solutions in scala while Josh is working with ruby. Currently, I have completed 3 of the problems we have decided to do:
This problem is to write an algorithm that will compute the digits of pi to 'n' precision (digits). Currently, my solution supports up to 16 digits of precision for pi and I have yet to find the effort to support more. I might come back and re-visit this one in the future when I'm more familiar with floating point precision in scala.

Example execution with 10 digits of precision:
 S:\Workspaces\Github\scala-projects\Numbers\scala>scala CalcPi.scala 10  

With this problem, the user enters a number 'n' where 'n' can either mean to calculate the Fibonacci sequence up to the Nth term or calculate the Fibonacci sequence up to 'n'. My solution works fine except that with large inputs of 'n', it takes exponential time to compute due to the recursive nature of the Fibonacci sequence.

Example execution of the first 15 Fibonacci numbers:
 S:\Workspaces\Github\scala-projects\Numbers\scala>scala Fibonacci.scala 15 Nth  
 The first 15 Fibonacci numbers are:  

Example execution of the Fibonacci sequence up to 1000:
 S:\Workspaces\Github\scala-projects\Numbers\scala>scala Fibonacci.scala 1000 toN  
 The Fibonacci numbers up to 1000 are:  

In this problem, a user is allowed to enter a number 'n' and the program will output all of the prime factors of 'n'. The algorithm I used to computer whether or not a number was prime I found on Wikipedia.

Example execution with 'n' = 113679:
 S:\Workspaces\Github\scala-projects\Numbers\scala>scala PrimeFactors.scala 113679  
 The prime factors of 113679 are:  

Hopefully Josh and I will continue working on these sets of problems for the foreseeable future because they are fun, simple, and definitely are a huge help in learning a new programming language.

No comments:

Post a Comment