February 2015 | Virtual Species

Nth Fibonacci number using Java Recursion

There are certain problems that just make sense to solve using Java Recursion. Demonstrating Fibonacci Series is one of them. Let’s take a look at something called Fibonacci series. Here are the first few numbers of this series:
0, 1, 1, 2, 3, 5, 8, 13, 21…

     Now stop reading and try to figure out what’s going on or let’s walk thru the sequence together and see how it works:
To obtain the sequence, we start with 0 and 1 after which every number is the sum of the previous two numbers. 
For example, the first two numbers will be 0 and 1
0, 1
For the next number, we add the previous two numbers 0 and 1 which gives one. 
0, 1, 1
The next number would be 1 + 1 = 2 
0, 1, 1, 2 
Now, the next number would be 1 + 2 = 3 
0, 1, 1, 2, 3

Russian Peasant Multiplication

This method involves a process of halving and doubling, which reduces one factor to powers of two and uses the distributive property of multiplication over addition to calculate a product.

Multiplication

                              25                    37
                              12                    74
                              6                      148
                              3                      296
                              1                      592
                            -------------------------           Therefore, 37 + 296 + 592 = 925
                                                      925                              ( 25 x 37 = 925 )