CODING JOY

The stunning potpourri of coding and mundane life

PERRIN SEQUENCE IN C LANGUAGE



AMRUHA AHMED
6th 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:

                
                    #include<stdio.h>
                    void main()
                    {
                        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
                        printf("\n Enter the number of terms:"); 
                        scanf("%d",&n);
                        printf("\n The Perrin Sequence is ....");
                        printf("\n %d \t %d \t %d",p0,p1,p2);
                        pn=p1+p0;//p(n)=p(n-2)+p(n-3)
                        while(ctr<n)
                            {
                                //displaying n'th term
                                printf("\t %d",pn);
                                //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