Java Recursion Primer Tutorial

Java Recursion Primer Tutorial

Java Recursion Primer Tutorial

Recursion is the process of repeating items in a self-similar way. For instance, when the surfaces of two mirrors are exactly parallel with each other the nested images that occur are a form of infinite recursion. The term has a variety of meanings specific to a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics and computer science, in which it refers to a method of defining functions in which the function being defined is applied within its own definition. Specifically this defines an infinite number of instances (function values), using a finite expression that for some instances may refer to other instances, but in such a way that no loop or infinite chain of references can occur. The term is also used more generally to describe a process of repeating objects in a self-similar way.

Recursion is not just applicable to Java. It is a good technique to use in many programming applications unless its use becomes too esoteric. It is usually true that a non-recursive technique can be used in place of the recursive technique. As you might guess, you can reduce the size of your code using recursion in your code. Recursive functions need an “anal” hard coded return, ahead of time, for the end-of-the-line value returned … as you see here for when i=1 or i=0 … if you don’t do this, it may be that your recursive function contributes to infinite looping … the stuff of nightmares.

Link to Recursion information … via Wikipedia (as per information above).

This tutorial shows the use of a recursive function used to show the Fibonacci Sequence of a designated sequence size.

Liber Abaci also posed, and solved, a problem involving the growth of a population of rabbits based on idealized assumptions. The solution, generation by generation, was a sequence of numbers later known as Fibonacci numbers. The number sequence was known to Indian mathematicians as early as the 6th century,[10][11][12] but it was Fibonacci’s Liber Abaci that introduced it to the West.

In the Fibonacci sequence of numbers, each number is the sum of the previous two numbers, starting with 0 and 1. This sequence begins 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987 … [13]

The higher up in the sequence, the more closely the ratio of two consecutive Fibonacci numbers will approach the golden ratio (approximately 1 : 1.618 or 0.618 : 1).

Link to Fibonacci Sequence information … via Wikipedia (as per information above).

Link to … Elementary my dear Watson! … wow.

Download programming code and rename to fibonacciRecursive.java

If this was interesting you may be interested in this too.

This entry was posted in eLearning, Tutorials and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>