Saturday, September 21, 2013

Implementation of "Triangular Number Sequence" with Scala

Triangular Number Sequence is 1, 3, 6, 10, 15. This is very basic mathematical concept.
This sequence is getting generated from a pattern of dots which form a Triangle.

To know more about Triangular Number Sequence, please check this page => Triangular Number Sequence

At this page only I saw one very good example of how we can use this sequence in solving problems. One problem is given @: A Walk in The Desert

So I thought of solving the problem with Functional Programming i.e. Scala. 
The solution code is @ Solution to A Walk in The Desert

The strategy I followed:
  • Consideration: My starting position is {0, 0} [FYI: Always first X co-ordinate and then Y co-ordinate]
  • Strategy:
  • In every odd step => Either I am going towards North or toward South i.e. my X co-ordinate is constant
  • In every even step => Either I am going towards East or toward West i.e. my Y co-ordinate is constant
  • In every steps, my co-ordinate(either X or Y based on whether my step is even or odd) will  change the sign.
Feel free to provide your comment if you see any scope of improvement. Thanks.

0 comments:

Post a Comment