Select Page

Series Program in Java

The series program in Java is written to print the mathematical series such as the Fibonacci series, Pell series, etc. A few of the renowned series are mentioned below.

• Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, …
• Pell series: 0, 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378, 5741, 13860, 33461, …
• Catalan number series: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, …

Note: We have started the Catalan series with the 0th term.

• Triangular number series: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, …
• Square number series: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, …
• Cube number series: 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, …

We will discuss each of the series mentioned above. Let’s begin with the Fibonacci series.

Fibonacci Series

Fibonacci series is a series whose next term is the summation of the last two terms. The first two terms of the series are 0 and 1. Thus, the third term is the addition of the first and the second term, i.e., 0 + 1 = 1, the fourth term becomes 1 + 1 = 2, and the fifth term becomes 1 + 2 = 3. Similarly, we can easily find the rest of the terms.

Pell Series

Pell series is the series whose next term is the addition of the second last term and the twice of the last term. The first two terms of the series are 0 and 1, respectively. Hence, the third term becomes summation of the first term and the twice of the second term, i.e., 0 + 1 * 2= 2, the fourth term is 1 + 2 * 2= 5, and the fifth term is 2 + 5 * 2= 12, and so on. Thus, if we have to find the nth term (Tn), we can represent it as:

Tn = Tn – 2 + (Tn – 1 * 2), where n > 2.

The following Java code uses the above mathematical relation to print the terms of the Pell series.

Using Java for Loop

Filename: PellSeriesIterative.java

Output:

Explanation: In the above program, the variables t1 and t2 are representing the first two terms of the Pell series. The noOfTerms variable is controlling up to which term the Java for-loop is displaying the Pell series. From the 3rd iteration, we are calculating the next term of the Pell series using the mathematical relation defined above the code.

Using Recursion

Filename: PellSeriesRecursive.java

Output:

Explanation: In the code, the iteration of the Java for-loop depends on the value of the variable noOfTerms. In our case, the loop is iterating 15 times (0 to 14). When the value of the loop variable i is 0, we display the first term of the Pell series. For i = 1, we display the second term of the series, and so on. In each iteration, we are invoking the method pellTerms() and storing the result. The method pellTerms() is finding the (i + 1)th term, recursively, using the mathematical relation defined above.

Catalan Number Series

Catalan number series is a series of natural numbers that are used in various interesting problems where counting is required. Using Combination, the series is represented as:

2nCn, where n >= 0. On further simplification we get,

, where n >= 0    where n >= 0.

Thus, for n = 0, we get:

=  = =  = 1

For n = 1, we get:

=  = =  = 1

For n = 2, we get:

=  = =  = 2

For n = 3, we get:

=  = =  = 5

Similarly, we can calculate for any value of n.

The following Java program uses the Java for-loop to display the Catalan number series with the help of the above mathematical formula.

Using Java for-loop

Filename: CatalanSeriersIterative.java

Output:

Explanation: Similar to the above examples, this time also, the variable noOfTerms is controlling the number of terms being displayed on the console. Notice, in the loop, the variable term is of type long, which is used for displaying the terms of the series. Instead of the variable of type long, we cannot use a variable of type integer because the numbers are of bigger size when we display the higher terms of the Catalan series. Instead of using a long variable for displaying terms, still, we have to take care of the overflow, i.e., the number can be bigger than the capacity of a long variable. For example, if we have calculated 2n! factorial separately, then for n = 12, we have to calculate 24! which is equal to 620448401733239439360000.  Now, this big number is impossible to store in a long variable. Therefore, we have used two for loops of calculating the terms where we are doing the multiplication and division process side by side so that the resultant number does fit in a long variable.

Using Recursion

Before writing the code, let us define the recursive formula for calculating the numbers of the Catalan series.

T(0) = 1 [Base case]

And,

T(n) = T(i)  T(n – i – 1), for n >= 1

Thus,

T(1) = T(i)  T(1 – i – 1) = T(0)  T(1 – 0 – 1) = T(0)  T(0) = 1  1 = 1

T(2) = T(i)  T(2 – i – 1) = T(0)  T(2 – 0 – 1) + T(1)  T( 2 – 1 – 1)

= T(0)  T(1) + T(1)  T(0) = 1  1 + 1  1 = 1 + 1 = 2

Similarly, other terms can also be calculated.

Filename: CatalanSeriesRecursive.java

Output:

Explanation: For each term, starting from the 0th term, we are invoking the method catalnNumbers(), and recursively finding the term of the Catalan series. The recursion used in the code is based on the recursive formula, which we have defined above.

### Application of Catalan Numbers

Catalan numbers are used in many interesting problems where counting is required. A few of them are mentioned below.

• Count the number expressions where n pair of braces are correctly matched.
• For n = 1, valid expression is (). Thus, the count is 1.
• For n = 2, valid expressions are () (), (()). Thus, the count is 2.
• For n = 3, valid expressions are () () (), (()) (), () (()), (() ()), ((())), Thus the count is 5.

• Count the number of different Binary Search Trees for n nodes.
• For n = 2, there are 2 different Binary Search Trees. The following diagram demonstrates the same.
• For n = 3, there are 5 different Binary Search Trees. Observe the following diagram.

### Triangular Number Series

Triangular number series is a series of numbers in triangular pattern. A triangular number is a number that counts the number of objects arranged in an equilateral triangle. Thus, the nth triangular number is the equilateral triangle who’s each side is comprised of n dots/ circle. The following diagram represents the same.

The nth triangular number is also the sum of natural numbers from 1 to n. Mathematically, the nth triangular number is represented by T(n) = (n  (n + 1)) / 2. Thus, T(1)  = (1  (1 + 1)) / 2 = (1  2) / 2 = 2 / 2 = 1

T(2) = (2  (2 + 1)) / 2 = (2  3) / 2 = 6 / 2 =  3

T(3) = (3 (3 + 1)) / 2 =  (3  4) / 2 = 12 / 2 = 6

Similarly, other triangular numbers can also be calculated. Now, let us observe the code for generating the triangular numbers.

Using Java for-loop

Filename: TriangularSeriesIterative.java

Output:

Explanation: In the code, we have used Java for-loop for calculating the triangular numbers till the 20th term. In each iteration, we are calculating the triangular number with the help of the mathematical formula define above. Now, let us learn the recursive approach.

Using Recursion

Filename: TriangularSeriesRecursive.java

Output:

Explanation: In the code, the Java for-loop is iterating 20 times for displaying the first 20 numbers of the triangular series. In each iteration, the method triangularNo() is called for calculating the terms of the triangular series. In the method triangularNo(), we are recursively calculating the sum of the natural number till the ith term. The ith term is passed as an argument to the method triangularNo(). The recursion is based on the fact that the nth triangular number is the sum of the first n natural numbers.

Square Number Series

Numbers that are perfect square form the square number series. Mathematically, the terms of the Square number series are represented by,

T(n) = n2, where n is the nth term

Thus, for n = 1

T(1) = 12 = 1  1 = 1

For n = 2,

T(2) = 22 = 2  2 = 4

For n = 3,

T(3) = 32 = 3  3 = 9

For n = 4,

T(4) = 42 = 4  4 = 16

In the similar way, we can calculate the other terms of the Square series. Let’s observe the code to generate the terms of the triangular series.

Using Java for-loop

Filename: SquareSeriesIterative.java

Output:

Explanation: In the code, we are printing the first 20 terms of the Square series using the Java for-loop. In the body of the for-loop, we have implemented the mathematical formula defined above.

Using Recursion

Let’s define the recursive relation to generated the square numbers. The recursion relation is:

T(1) = 1   [Base case]

And,

T(n) = (2  (n – 1) + 1) + T(n – 1), for n >= 2

Thus,

T(2) = (2  ( 2 – 1) + 1) + T(2 – 1) = (2  1 + 1) + T(1) = 3 + 1 = 4

T(3) = (2  ( 3 – 1) + 1) + T(3 – 1) = (2  2 + 1) + T(2) = 5 + 4 = 9

T(4) = (2  ( 4 – 1) + 1) + T(4 – 1) = (2  3 + 1) + T(3) = 7 + 9 = 16

The other terms can be calculated in the same way.

Filename: SquareSeriesRecursive.java

Output:

Explanation: Similar to the iterative approach, here also, we are printing the first 20 terms of the Square series. In each iteration, we are invoking the method sqrNo() and displaying the results generated by the method sqrNo(). In the method sqrNo(), we are calculating the terms of the Square series using the recursive relation explained above.

Cube Number Series

Numbers that are perfect cubes form the Cube number series. Mathematically, the terms of the Cube numbers series are represented by:

T(n) = n3, where n is the nth term

Thus, for n = 1, we have

T(1) = 13 = 1  1  1 = 1

For n = 2,

T(2) = 23 = 2  2  2 = 8

For n = 3,

T(3) = 33 = 3  3  3 = 27

For n = 4,

T(4) = 43 = 4  4  4 = 64

Similarly, we can easily calculate the other terms of the Cube series.

Now, let’s learn about the program to print the terms of the Cube series. We will start with the iterative approach.

Using for loop

Filename: CubeSeriesIterative.java

Output:

Explanation: Similar to the iterative approaches of other series, we are calculating the terms of the Cube series with the help of the mathematical formula defined above. This time also, the Java for-loop iterates 20 times.

Now let’s observe how we can do the same thing using recursion.

Using Recursion

Before writing the code, it is important to know about the recursive relation for calculating the terms of the Cube series. The recursive relation is:

T(1) = 1 [Base case]

T(n) = T(n – 1) + 1 + 3  (n – 1)  (n – 1)  1 + 3  (n – 1)  1  1, for n >= 2

Thus, for n = 2, we get

T(2) = T(2 – 1) + 1 + 3  (2 – 1)  (2 – 1)  1 + 3 (2 – 1)  1  1

= T(1) + 1 + 3  1  1  1 + 3  1  1  1 = 1 + 1 + 3 + 3 = 8

For n = 3,

T(3) = T(3 – 1) + 1 + 3  (3 – 1)  (3 – 1)  1 + 3 (3 – 1)  1  1

= T(2) + 1 + 3  2  2  1 + 3  2  1  1 = 8 + 1 + 12 + 6 = 27

For n = 4,

T(4) = T(4 – 1) + 1 + 3  (4 – 1)  (4 – 1)  1 + 3 (4 – 1)  1  1

= T(3) + 1 + 3  3  3  1 + 3  3  1  1 = 27 + 1 + 27 + 9 = 64

In the same way, we can calculate the other terms of the series very easily.

Note: The above recursive relation is derived using the following mathematical identity

(a + 1)3 = a3 + 13 + 3.a2.b + 3.a.b2

The following Java code uses the recursive relation to calculate the terms of the series.

Filename: CubeSeriesRecursion.java

Output:

Explanation: In the code, the Java for-loop is again iterating 20 times, and for each iteration, the method cubeNo() is invoked, which takes the loop variable i in the argument. For each value of the variable i, we are calculating the cube of i using the recursive formula defined above.