CODING JOY

The stunning potpourri of coding and mundane life

GENERATING PADOVAN SEQUENCE USING PYTHON



AMRUHA AHMED
14th April,2023.



The Padovan Sequence is a special type of sequence in which the n’th term is determined by the recurrence relation;
P(n)=P(n-2)+P(n-3)
Where P(0),P(1) and P(2) are initialized to 1.
The following figure, in which the length of the current triangle's side is determined by the sum of the lengths of the sides of those triangles that are touching it, can also be used to understand the Padovan Sequence (assuming that this spiral of equilateral triangle is drawn from inside out).

blog1
There can be two ways to generate the Padovan Sequence:

In this blog post , I will be discussing how to generate the Padovan Sequence using the constraints mentioned above.


GENERATING PADOVAN SEQUENCE WHEN NUMBER OF TERMS ARE GIVEN:

Supposing the number of terms is given as 5, it means that the first 5 terms of the Padovan Sequence need to be printed on the screen.


VARIABLES REQUIRED:

ALGORITHM:

The following is the code written in Python to print the Padovan Sequence when the number of terms is provided by the user.


PROGRAM:

                
                    limit=int(input("Enter the number of terms:"))#number of terms
                    p0=1#first term of the series
                    p1=1#second term of the series
                    p2=1#third term of the series
                    pn=p0+p1#current term of the series
                    ctr=3#count of digits printed
                    print("{}\n{}\n{}".format(p0,p1,p2))
                    while(ctr<limit):
                      print("{}".format(pn))
                      ctr=ctr+1
                      p0=p1
                      p1=p2
                      p2=pn
                      pn=p0+p1                          
                    
                     
             
         
CODE COPIED

DRY RUN:


Supposing limit is given as 5.Then the values of p0,p1,p2,pn and ctr will be as follows:
blog1

The while loop ceases when value of ctr becomes greater than or equal to limit.



OUTPUT:

Enter the number of terms: 5


1 1 1 2 2



GENERATING PADOVAN SEQUENCE WHEN LAST TERM OF THE SEQUENCE IS GIVEN:

Supposing the number limit or last term is given as 5, it means that all the terms in Padovan Sequence that are less than or equal to 5 will be printed on the screen.


VARIABLES REQUIRED:


ALGORITHM:


The following is the code written in Python to print the Padovan Sequence when the number limit of the sequence is provided by the user:

PROGRAM:

                       
                        
                             
                        n=int(input(("Enter a number:")))#number till which the series is printed
                        p0=1#first term of the series
                        p1=1#second term of the series
                        p2=1#third term of the series
                        pn=p1+p0#n'th term of the series
                        print("{}\n{}\n{}".format(p0,p1,p2))
                        while(pn<=n):
                          print("{}".format(pn))
                          p0=p1
                          p1=p2
                          p2=pn
                          pn=p0+p1

    
                    
                

DRY RUN:


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

The loop ceases when pn becomes greater than n.



OUTPUT:


Enter a number: 5


1 1 1 2 2 3 4 5