The program is written here to print the series
# One for loop is used to print the series.
# The initial three terms of the series is 1, 3 and 4.
# From the 4th term, the value of each term is the sum of previous three terms.
For example, let the input be 6.
1st term: 1
2nd term: 3
3rd term: 4
4th term: (4 + 3 + 1) = 8
5th term: (8 + 4 + 3) = 15
6th term: (15 + 8 + 4) = 27
Therefore, the output series will be:
1 3 4 8 15 27
INPUT: Number of terms
OUTPUT: The aforesaid series up to nth term
PROCESS:
Step 1: [Taking the input]
Read n [Number of terms]
Step 2: [Printing the series]
Set a<-1
Set b<-3
Set c<-4
Print "The series is: a, b, c"
For i=4 to n repeat
Set sum <- a+b+c
Print sum
Set a <- b
Set b <- c
Set c <- sum
[End of ‘for’ loop]
Step 3: Stop.
TIME COMPLEXITY:
for(i=4; i<=n; i++)--------------------------- O(n)
{
sum = a+b+c;
printf("%d ",sum);
a = b;
b = c;
c = sum;
}
The time complexity of this program is O(n) where ‘n’ is the number of terms of the series.
SPACE COMPLEXITY:
The space complexity of this program is O(1) as it requires a constant number of memory spaces for any given input.
Related