Series program in java

In this article, we will learn how to write various types of series programs in java. We will learn how to write the Fibonacci series program in java using different approaches. We will also learn how to write the Triangular number series program in java using different approaches. We will also learn how to write the Pell series program in java using various approaches.

What is the Fibonacci series?

In the Fibonacci series, the next term is an addition of the previous two terms. For example, if the previous two terms are 3 and 5 then the next term will be 8 (3+5). The first two terms of the Fibonacci series are 0 and 1 respectively. Now, if we look at the third term it will be the summation of the first term(0) and the second term(1) is 1 (0+1). If we look for the fourth term it will be the addition of the second term(1) and the third term (1) is 2(1+1). Using the same approach we can find the remaining terms easily. Let’s see various approaches to writing the Fibonacci series program in java.

Approach 1: Iterative method for the Fibonacci series program in java

In this iterative method, we will use the first two terms of the Fibonacci series 0 and 1, and then we will find further terms using these two terms. Let’s see how to write the Fibonacci series program in java using this method.

class PrepBytes {
    public static void main(String[] args) {
        int first_term=0;
        int second_term=1;
        int second_last_term=0;
        int last_term=1;
        System.out.println("The first 10 terms of the Fibonacci series are:");
        for(int i=0;i<10;i++){
            if(i==0){
                System.out.print(first_term+" ");
            }
            else if(i==1){
                System.out.print(second_term+" ");
            }
            else{
                int current_term=second_last_term+last_term;
                System.out.print(current_term+" ");
                second_last_term=last_term;
                last_term=current_term;
            }
        }
    }
}

Output

The first 10 terms of the Fibonacci series are:
0 1 1 2 3 5 8 13 21 34 

In the above program, first, we have defined the initial two terms 0 and 1. After that, we have also created two more variables last_term and second_last_term to keep track of the last and second last term so far. Now, we will run a for loop 10 times to print the first 10 terms of the Fibonacci series. In the loop, if i is less than 2 then we will directly print the first two terms, and if i is greater than or equal to 2 then first we will compute the current term which equals the sum of the last and second last term. Now, we will change the second last term as the last term and the last term as the current term to calculate further terms. In the output, we can see that the first ten terms of the Fibonacci series are printed.

Time Complexity: O(n)
Auxiliary Space Complexity: O(1)

Approach 2: Recursive method for the Fibonacci series program in java

In this approach, we will use base case as the first two terms and our recursive formula for the nth term will be (n-1)th term +(n-2)th term. Let’s see how to write the Fibonacci series program in java using the recursive method.

class PrepBytes {
    static int fibonacci(int n)
    {
        if (n < 2)
            return n;

        return fibonacci(n - 1)
            + fibonacci(n - 2);
    }
    public static void main(String args[])
    {
        System.out.println("The first 10 terms of the Fibbonaci series are:");
        for (int i = 0; i < 10; i++) {

            System.out.print(fibonacci(i) + " ");
        }
    }
}

Output

The first 10 terms of the Fibonacci series are:
0 1 1 2 3 5 8 13 21 34 

In the above program, we have written a recursive method Fibonacci. In that method, we have given base case (recursion termination condition) if the given number is less than 2 then we will return that number. Otherwise, we will find the n-1 term [fibonacci(n-1)] and the n-2 term [fibonacci(n-2)] and we will return the sum of both terms. In the output, we can see that the first ten terms of the Fibonacci series are printed.

Time Complexity: O(2^n)
Auxiliary Space Complexity: O(1)

Approach 3: Recursion + Memoization method for the Fibonacci series program in java

In this method, we will use the above recursion method the only difference is that we will use the memoization method with this approach. Memoization means we will store the answer to all the calculated terms so that we can use them again and we do not need to find that term again and again.

class PrepBytes {
    static int fibonacci(int n,int[] memo)
    {
        if (memo[n]!=-1){
            return memo[n];
        }
        if (n < 2)
            return n;

        int current_term= fibonacci(n - 1,memo) + fibonacci(n - 2,memo);
        memo[n]=current_term;
        return current_term;
    }
    public static void main(String args[])
    {
        int[] memo= new int[10];
        for(int i=0;i<10;i++){
            if(i<2){
                memo[i]=i;
            }    
            else{
                memo[i]=-1;
            }
        }
        
        System.out.println("The first 10 terms of the Fibbonaci series are:");
        for (int i = 0; i < 10; i++) {

            System.out.print(fibonacci(i,memo) + " ");
        }
    }
}

Output

The first 10 terms of the Fibonacci series are:
0 1 1 2 3 5 8 13 21 34 

In the above program, we have created an array of size 10 to store calculated terms and we will initialize the first two terms as 0 and 1 in that array. After that, we will use the same recursive method as above to find the nth term of the Fibonacci series. Now, when we call a method fibonacci to find the nth term we have two options. One is, if the nth term is already calculated then we will directly return the answer. Otherwise, we will calculate the answer and store the answer in an array while returning from the recursive call. In the output, we can see that the first ten terms of the Fibonacci series are printed.

Time Complexity: O(n)
Auxiliary Space Complexity: O(n)

What is the Triangular number series?

When numbers are in triangular patterns it is called the Triangular number series. We can say a number is a triangular number if we can represent it in form of a triangular grid. Let’s understand what is the Triangle number series.

In the above image, we can see that, we can put only one dot in the triangle so our first term will be 1. In the second triangle, we can place three dots so our second term will be 3. In the third and fourth triangles, we can place 6 and 10 dots respectively. So our third term is 6 and the fourth term is 10.

Observation: the nth term is the sum of the first n natural numbers.

T(1) = 1 = 1
T(2) = 1+2 = 3
T(3) = 1+2+3 = 6
T(4) = 1+2+3+4 =10
.
.
.
T(n) = the sum of the first n natural number

*The equation to find the sum of the first n natural numbers: (n(n+1))/2**

Let’s see various approaches to writing the Triangular number series program in java.

Approach 1: Iterative method for the Triangular number series program in java

In this iterative approach, we will run a loop to find all the numbers of the Triangular number series using the above equation. Let’s see how to write iterative code for the Triangular series program in java.

class PrepBytes {
    public static void main(String[] args) {
        int N=10;
        System.out.println("The first 10 terms of the Triangular number series:");
        for(int i=1;i<=N;i++){
            int current_term=(i*(i+1))/2;
            System.out.print(current_term+" ");
        }
        
    }
}

Output

The first 10 terms of the Triangular number series:
1 3 6 10 15 21 28 36 45 55 

In the above program, we run a loop from 1 to 10. During each iteration, we will find the current ith term using the above equation. In the output, we can see that the first ten terms of the Triangular number series are printed.

Approach 2: Recursive method for the Triangular number series program in java

In this recursive method, we will use recursion to find the current term of the Triangular number series rather than the equation. Let’s see how to write iterative code for the Triangular series program in java.

class PrepBytes {
    static int triangular_number(int n){
        //base case
        if(n==1){
            return 1;
        }
        else{
            // recursive formula
            return n+triangular_number(n-1);
        }
    }
    
    public static void main(String[] args) {
        int N=10;
        System.out.println("The first 10 terms of the Triangular number series:");
        for(int i=1;i<=N;i++){
            int current_term=triangular_number(i);
            System.out.print(current_term+" ");
        }
        
    }
}

Output

The first 10 terms of the Triangular number series:
1 3 6 10 15 21 28 36 45 55 

In the above program, we have created a recursive method triangular number to find the ith term of the Triangular number series. In this recursive method, our base case will be for the first term as we know the answer to the first term is 1. Our recursive relation will be the summation of the current term and remaining terms (n-1). In the output, we can see that the first ten terms of the Triangular number series are printed.

What is the Pell series?

Pell series starts with the first term as 0 and the second term as 1. The next term will be an addition to the second last term and two times to the last term. Now, if we compute the third term it will be the sum of the second last term (0), and twice of the last term (21) is 2 (0+2). The fourth term will be the sum of the second last term (1) and twice of the last term (22) is 5 (1+4). We can find more terms using the same method.

*Relation of Pell series number: T(n) = T(n-2) + 2T(n-1)**

T(1) = 0
T(2) = 1
T(3) = 0+2*1 = 0+2 = 2
T(4) = 1+2*2 = 1+4 = 5
T(5) = 2+2*5 = 2+10 = 12
.
.
.
T(n) = T(n-2) + 2*T(n-1)

Let’s see various approaches to writing the Pell series program in java.

Approach 1: Iterative method for the Pell series program in java

In this iterative approach, we will run a for loop to find every term of the Pell series. Let’s see how to write iterative code for the Pell series program in java.

class PrepBytes {
    public static void main(String[] args) {
        int first_term=0;
        int second_term=1;
        int second_last_term=0;
        int last_term=1;
        System.out.println("The first 10 terms of the Pell series are:");
        for(int i=0;i<10;i++){
            if(i==0){
                System.out.print(first_term+" ");
            }
            else if(i==1){
                System.out.print(second_term+" ");
            }
            else{
                int current_term=second_last_term+2*last_term;
                System.out.print(current_term+" ");
                second_last_term=last_term;
                last_term=current_term;
            }
        }
    }
}

Output

The first 10 terms of the Pell series are:
0 1 2 5 12 29 70 169 408 985 

In the above program, first, we have defined the initial two terms 0 and 1. After that, we have also created two more variables last_term and second_last_term to keep track of the last and second last term so far. Now, we will run a for loop 10 times to print the first 10 terms of the Fibonacci series. In the loop, if i is less than 2 then we will directly print the first two terms, and if i is greater than or equal to 2 then first we will compute the current term which equals the sum of twice of the last and second last term. Now, we will change the second last term as the last term and the last term as the current term to calculate further terms. In the output, we can see that the first ten terms of the Fibonacci series are printed.

Time Complexity: O(n)
Auxiliary Space Complexity: O(1)

Approach 2: Recursive method for the Pell series program in java

In this approach, we will use base case as the first two terms and our recursive formula for the nth term will be (n-2)th term + 2*(n-1)th term. Let’s see how to write the Pell series program in java using the recursive method.

class PrepBytes {
    static int pell(int n)
    {
        if (n < 2)
            return n;

        return pell(n - 2) + 2* pell(n-1) ;
    }
    public static void main(String args[])
    {
        System.out.println("The first 10 terms of the Pell series are:");
        for (int i = 0; i < 10; i++) {

            System.out.print(pell(i) + " ");
        }
    }
}

Output

The first 10 terms of the Pell series are:
0 1 2 5 12 29 70 169 408 985 

In the above program, we have written a recursive method pell. In that method, we have given base case (recursion termination condition) if the given number is less than 2 then we will return that number. Otherwise, we will find the 2n-1 term [2pell(n-1)] and the n-2 term [pell(n-2)] and we will return the sum of both terms. In the output, we can see that the first ten terms of the Pell series are printed.

Time Complexity: O(2^n)
Auxiliary Space Complexity: O(1)

Leave a Reply

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