CODING JOY

The stunning potpourri of coding and mundane life

GENERATING PRIME PADOVAN SEQUENCE USING C++



AMRUHA AHMED
8th May,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

The task at hand is to generate prime padovan sequence till a number; to display only those terms occurring before the limit which are prime as well as a part of the padovan sequence.


VARIABLES REQUIRED:
ALGORITHM:

The following is the code written in C++ language to print the Prime Padovan Sequence when the number limit is provided by the user.


PROGRAM:

                
                    #include<iostream>
                    using namespace std;
                    int main()
                    {
                        int n;//number till which the series need to be printed
                        int i;//loop counter
                        int ctr;//count of factors of a number
                        cout<<"enter a number:";
                        cin>>n;
                        int p0=1;//first term of 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
                        while(pn<=n)
                        {
                            ctr=0;
                            for(i=1;i<=pn;i++)
                            {
                                if(pn%i==0)
                                ctr++;
                            }
                            if(ctr==2)
                            cout<<"\t"<<pn;
                            p0=p1;
                            p1=p2;
                            p2=pn;
                            pn=p0+p1;
                            
                        }
                        return 0;
                    }
                    
                     
             
         
CODE COPIED

DRY RUN:


The dry run is very similar to the one in Padovan Sequence. Additionally, the value of "pn" is checked to see if it is prime or not. For more details about the dry run of Padovan Sequence , Click Here

OUTPUT:

enter a number:7


2 2 3 5 7