FOR FREE YEAR SOLVED


Description

# The program is written here to find the sum of the series. 

 

# The number of terms is taken as input. 

 

# From the 3rd term, the value of each term is the sum of the previous three terms. 

 

 

If the number of terms is 5, then, the sum of the series will be:

Algorithm

INPUT: The number of terms

OUTPUT: The sum of the series

PROCESS:

Step 1: [Taking the input]

               Read n [number of terms]

Step 2: [Calculating the sum of the series]

               Set a<-0

               Set b<-1

               Set c<-2

               Set s<-3

               [If the number of terms is 1]

               If n=1 then

                              Print "The sum is: b"

               [If the number of terms is 2]

               Else if n=2 then

                              Print "The sum is: (b+c)"

               [If the number of terms is more than 2]

               Else

                              For i=3 to n repeat

                                             [Adding the three previous digits to get the sum]

                                             Set d<-a+b+c

                                             [Adding the value to the sum of the series]

                                             Set s<-s+d

                                             Set a<-b

                                             Set b<-c

                                             Set c<-d

                              [End of ‘for’ loop]

                              Print "The sum of the series is: s"

               [End of ‘if-else-if’]

Step 3: Stop.

Code

TIME COMPLEXITY:

if(n==1)---------------------------------------------- O(1)

                              printf("The sum is: %d",b);

               //if the number of terms is 2

               else if(n==2)---------------------------------------- O(1)

                              printf("The sum is: %d",b+c);

               //if the number of terms is more than 2

               else

               {

                              for(i=3;i<=n;i++)-------------------------- O(n)

                              {

                                             //adding the three previous digits to get the sum

                                             d=a+b+c;

                                             //adding the value to the sum of the series

                                             s=s+d;

                                             a=b;

                                             b=c;

                                             c=d;

                              }

                              printf("The sum of the series is: %d",s);

               }

 

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.