PERRIN SEQUENCE IN C LANGUAGE
AMRUHA AHMED
6th 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.
VARIABLES REQUIRED:
- p0-first term of the sequence
- p1second term of the sequence
- p2-third term of the sequence
- pn-n'th term of the sequence
- n-number of terms to be displayed on the screen
- ctr-number of terms displayed currently
ALGORITHM:
- 1.Start
- 2.Accept the number of terms till which the series needs to be printed and store in ānā
- 3.Initialize the values of 'p0','p1','p2' to 3,0 and 2,respectively
- 4.Initialize the counter 'ctr' to 3, since the first three terms are going to be printed before entering the while loop
- 5.In order to generate the n'th term, calculate p0+p1 and store it in pn
-
6.While 'ctr' does not exceed 'n' , the following steps need to be repeated:
- (i) display the value of 'pn'
- (ii) Assign the value of 'p1' to 'p0'
- (iii) Assign the value of 'p2' to 'p1'
- (iv)Assign the value of 'pn' to 'p2'
- (v) Update the value of 'pn' as pn=p0+p1
- 7.Stop
PROGRAM:
#include<stdio.h>
void main()
{
int p0=3;//first term of the sequence
int p1=0;//second term of the sequence
int p2=2;//third term of the sequence
int pn;//n'th term of the sequence
int n;//number of terms to be displayed on the screen
int ctr=3;//number of terms displayed currently
printf("\n Enter the number of terms:");
scanf("%d",&n);
printf("\n The Perrin Sequence is ....");
printf("\n %d \t %d \t %d",p0,p1,p2);
pn=p1+p0;//p(n)=p(n-2)+p(n-3)
while(ctr<n)
{
//displaying n'th term
printf("\t %d",pn);
//updating the terms of the sequence for the next iteration
p0=p1;
p1=p2;
p2=pn;
pn=p0+p1;
// updating ctr value
ctr++;
}
}
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:
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