CODING JOY

The stunning potpourri of coding and mundane life

PERRIN SEQUENCE IN PYTHON 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:

                
                    p0=3 #first term of the sequence
                    p1=0 #second term of the sequence
                    p2=2 #third term of the sequence
                    ctr=3 #number of terms displayed currently
                    pn=p0+p1 #n'th term of the sequence
                    n=int(input("Enter the number of terms:")) #number of terms to be displayed on the screen
                    print("The Perrin Sequence is....")
                    print("{}\n{}\n{}".format(p0,p1,p2))
                    while(ctr<n):
                        #displaying n'th term
                        print("{}".format(pn))
                        # updating the terms of the sequence for the next iteration
                        p0=p1
                        p1=p2
                        p2=pn
                        pn=p0+p1
                        # updating ctr value
                        ctr=ctr+1   

             
         
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