CODING JOY

The stunning potpourri of coding and mundane life

PERRIN SEQUENCE IN JAVA LANGUAGE



AMRUHA AHMED
7th October,2023.


The Perrin Sequence, also known as the Skiponacci Sequence, is an integer sequence whose n’th term abides by the following recurrence relation :
P(N)=P(N-2)+P(N-3)
Where P(0)=3, P(1)=0 and P(2)=2 are the initial conditions.

blog1

VARIABLES REQUIRED:



ALGORITHM:


PROGRAM:

                
                    import java.util.*;
                    class perrinsequence
                    {
                        void main()
                        {
                            Scanner ob=new Scanner(System.in);
                            int p0=3;//first term of the sequence
                            int p1=0;//second term of the sequence
                            int p2=2;//third term of the sequence
                            int pn;//n'th term of the sequence
                            int n;//number of terms to be displayed on the screen
                            int ctr=3;//number of terms displayed currently
                            System.out.println("Enter the number of terms:");
                            n=ob.nextInt();
                            System.out.println("The Perrin Sequence is ....\n");
                            System.out.print(p0+"\t"+p1+"\t"+p2+"\t");
                            pn=p1+p0;//p(n)=p(n-2)+p(n-3)
                            while(ctr<n)
                            {
                                //displaying n'th term
                                System.out.print(pn+"\t");
                                //updating the terms of the sequence for the next iteration
                                p0=p1;
                                p1=p2;
                                p2=pn;
                                pn=p0+p1;
                                // updating ctr value
                                ctr++;
                                
                            }
                        }
                    }


             
         
CODE COPIED

DRY RUN:

Supposing the input of number limit that is 'n' is provided as 6 then the values of p0,p1,p2 and pn will be as follows:


blog1

The loop ceases when ctr becomes greater than or equal to n.


OUTPUT:

Enter the number of terms:6

The Perrin Sequence is ....

3 0 2 3 2 5