CODING JOY

The stunning potpourri of coding and mundane life

GENERATING PRIME PADOVAN SEQUENCE USING JAVA



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 Java language to print the Prime Padovan Sequence when the number limit is provided by the user.


PROGRAM:

                
                    import java.util.*;
                    class primepadovan
                    {
                        Scanner ob=new Scanner(System.in);
                        void main()
                        {
                            int n;//number till which the series is printed
                            int i;//loop counter
                            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
                            System.out.println("Enter a number:");
                            n=ob.nextInt();
                            while(pn<=n)
                            {
                                int ctr=0;//counter to count the number of factors 
                                for(i=1;i<=pn;i++)
                                {
                                    if(pn%i==0)
                                    ctr++;
                                    
                                }
                                if(ctr==2)
                                System.out.print("\t"+pn);
                                p0=p1;
                                p1=p2;
                                p2=pn;
                                pn=p0+p1;
                                
                            }        
                        }
                    }
                    
                     
             
         
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