CODING JOY

The stunning potpourri of coding and mundane life

GENERATING PADOVAN SEQUENCE USING C LANGUAGE



AMRUHA AHMED
11th 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 C language to print the Padovan Sequence when the number of terms is provided by the user.


PROGRAM:

                
#include<stdio.h>
void main()
{
    int limit;//number of terms
    printf("\n Enter the number of terms:");
    scanf("%d",&limit);
    int p0=1;//first term of the series
    int p1=1;//second term of the series
    int p2=1;//third term of the series
    int pn=p0+p1;//current term of the series
    int ctr=3;//count of digits printed
    printf("\n%d\t%d\t%d",p0,p1,p2);
    while(ctr<limit)
    {
        printf("\t%d",pn);
        ctr++;
        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 C language to print the Padovan Sequence when the number limit of the sequence is provided by the user:

PROGRAM:

                       
                        #include<stdio.h>
                        void main()
                        {
                            int n;//number till which the series is printed
                            printf("\n Enter a number:");
                            scanf("%d",&n);
                            int p0=1;//first term of the series
                            int p1=1;//second term of the series
                            int p2=1;//third term of the series
                            int pn=p0+p1;//n'th term of the series
                            printf("\n%d\t%d\t%d",p0,p1,p2);
                            while(pn<=n)
                            {
                                printf("\t%d",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