PERRIN SEQUENCE IN PYTHON LANGUAGE
AMRUHA AHMED
7th 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:
p0=3 #first term of the sequence
p1=0 #second term of the sequence
p2=2 #third term of the sequence
ctr=3 #number of terms displayed currently
pn=p0+p1 #n'th term of the sequence
n=int(input("Enter the number of terms:")) #number of terms to be displayed on the screen
print("The Perrin Sequence is....")
print("{}\n{}\n{}".format(p0,p1,p2))
while(ctr<n):
#displaying n'th term
print("{}".format(pn))
# updating the terms of the sequence for the next iteration
p0=p1
p1=p2
p2=pn
pn=p0+p1
# updating ctr value
ctr=ctr+1
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