Posted on by Kalkicode
Code Number

Reverse tribonacci series

The Tribonacci sequence is an extension of the Fibonacci sequence, where each term is the sum of the three preceding terms. In the reverse Tribonacci series, we start with the last four terms of the series and work our way backwards to the first term.

Problem Statement

Given a positive integer n, we are required to print the reverse Tribonacci series up to the n-th term.

For example, if n is 15, the reverse Tribonacci series would be:

927 504 274 149 81 44 24 13 7 4 2 1 1 0 0

In the above series, the last four terms (927, 504, 274, 149) are obtained by summing the preceding three terms (504 + 274 + 149 = 927). The next term (504) is obtained by summing the preceding three terms (927 + 504 + 274 = 1705), and so on.

Algorithm

1. Start the main function.

2. Define the function view_series that takes four parameters: first, second, third, and n.

3. If n is greater than 4, call the view_series function recursively with updated parameters.

4. Inside the view_series function, call the view_series function recursively with the values of second, third, and the sum of the three parameters.

5. Print the sum of the three parameters.

6. In the main function, call the view_series function with initial values of 0, 1, 1, and n.

7. Print the base cases:

  • If n is greater than 3, print 1.
  • If n is greater than 2, print 1.
  • If n is greater than 1, print 0.
  • If n is greater than 0, print 0.

8. End the main function.

Pseudocode


view_series(first, second, third, n):
	if n > 4:
		view_series(second, third, first + second + third, n - 1)
		print (first + second + third)

tribonacci(n):
	view_series(0, 1, 1, n)
	if n > 3:
		print 1
	if n > 2:
		print 1
	if n > 1:
		print 0
	if n > 0:
		print 0

main():
	n = 15
	tribonacci(n)

  

Explanation

The given code solves the problem of printing the reverse Tribonacci series up to the n-th term. It uses recursive functions to generate and print the series.

The view_series function is responsible for generating the series. It takes four parameters: first, second, third, and n. If n is greater than 4, the function calls itself recursively with the updated parameters. It then prints the sum of the three parameters.

The tribonacci function serves as the entry point. It calls the view_series function with initial values of 0, 1, 1, and n. It also handles the base cases by checking the value of n and printing the corresponding numbers.

In the main function, the value of n is set to 15, and the tribonacci function is called.

The output of the code for n = 15 is:

927 504 274 149 81 44 24 13 7 4 2 1 1 0 0

This output represents the reverse Tribonacci series up to the 15th term. Each term is obtained by summing the three preceding terms, starting from the last four terms.

Code Solution

Here given code implementation process.

//C Program
//Reverse tribonacci sequence
#include <stdio.h>

void view_series(int first,int second,int third,int n)
{

  if(n > 4)
  {

    view_series(second,third,first + second + third,n-1);

    printf("  %d",first + second + third);
  } 

}

void tribonacci(int n)
{

  view_series(0, 1, 1, n);
   //Base cases
  if(n > 3)
  {
    printf("  %d",1);
  }
  if(n > 2)
  {
    printf("  %d",1);
  }
 
  if(n > 1)
  {
    printf("  %d",0);
  }
  if(n > 0)
  {
    printf("  %d",0);
  }


}

int main()
{
 
  int n = 15;
  
  tribonacci(n);

  return 0;
}

Output

927  504  274  149  81  44  24  13  7  4  2  1  1  0  0 
//C++ Program
//Reverse tribonacci sequence

#include<iostream>

using namespace std;
class MyNumber {
public:
  void view_series(int first, int second, int third, int n) {
    if (n > 4) {
      this->view_series(second, third, first + second + third, n - 1);
      cout <<"  "<< (first + second + third);
    }
  }
  void tribonacci(int n) {
    this->view_series(0, 1, 1, n);
    if (n > 3) {
      cout << ("  1");
    }
    if (n > 2) {
      cout << ("  1");
    }
    if (n > 1) {
      cout << ("  0");
    }
    if (n > 0) {
      cout << ("  0");
    }
  }

};

int main() {
  MyNumber obj ;
  int n = 15;
  obj.tribonacci(n);
}

Output

927  504  274  149  81  44  24  13  7  4  2  1  1  0  0 
/*
  Java Program
  Reverse tribonacci sequence
*/

public class MyNumber {

  public void view_series(int first,int second,int third,int n)
  {

    if(n > 4)
    {

      view_series(second,third,first + second + third,n-1);

      System.out.print("  "+ (first + second + third));
    } 

  }

  public void tribonacci(int n)
  {
    
    view_series(0, 1, 1, n);
     //Base cases
    if(n > 3)
    {
      System.out.print("  1");
    }
    if(n > 2)
    {
      System.out.print("  1");
    }
   
    if(n > 1)
    {
      System.out.print("  0");
    }
    if(n > 0)
    {
      System.out.print("  0");
    }


  }
  public static void main(String[] args) {
    
    MyNumber obj = new MyNumber();

    int n = 15;
  
    obj.tribonacci(n);
  }


}

Output

927  504  274  149  81  44  24  13  7  4  2  1  1  0  0 
/*
  C# Program
  Reverse tribonacci sequence
*/
using System;
public class MyNumber {

	public void view_series(int first,int second,int third,int n)
	{

		if(n > 4)
		{

			view_series(second,third,first + second + third,n-1);

			Console.Write("  "+ (first + second + third));
		} 

	}

	public void tribonacci(int n)
	{

		view_series(0, 1, 1, n);
		//Base cases
		if(n > 3)
		{
			Console.Write("  1");
		}
		if(n > 2)
		{
			Console.Write("  1");
		}

		if(n > 1)
		{
			Console.Write("  0");
		}
		if(n > 0)
		{
			Console.Write("  0");
		}


	}
	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();

		int n = 15;

		obj.tribonacci(n);
	}


}

Output

927  504  274  149  81  44  24  13  7  4  2  1  1  0  0 
# Python Program
# Reverse tribonacci sequence

class MyNumber :
  def view_series(self, first, second, third, n) :
    if (n > 4) :
      self.view_series(second, third, first + second + third, n - 1)
      print((first + second + third),end="  ")
    
  
  def tribonacci(self, n) :
    self.view_series(0, 1, 1, n)
    if (n > 3) :
      print(1,end="  ")
    
    if (n > 2) :
      print(1,end="  ")
    
    if (n > 1) :
      print(0,end="  ")
    
    if (n > 0) :
      print(0,end="  ")
    
  
def main() :
  obj = MyNumber()
  n = 15
  obj.tribonacci(n)
  

if __name__ == "__main__":
  main()

Output

927  504  274  149  81  44  24  13  7  4  2  1  1  0  0 
# Ruby Program
# Reverse tribonacci sequence

class MyNumber 
	def view_series(first, second, third, n) 
		if (n > 4) 
			self.view_series(second, third, first + second + third, n - 1)
			print("  ", (first + second + third))
		end
	end
	def tribonacci(n) 
		self.view_series(0, 1, 1, n)
		if (n > 3) 
			print("  1")
		end
		if (n > 2) 
			print("  1")
		end
		if (n > 1) 
			print("  0")
		end
		if (n > 0) 
			print("  0")
		end
	end

end
def main() 
	obj = MyNumber.new()
	n = 15
	obj.tribonacci(n)
end
main()

Output

927  504  274  149  81  44  24  13  7  4  2  1  1  0  0 
/*
  Node JS Program
  Reverse tribonacci sequence
*/

class MyNumber {
	view_series(first, second, third, n) {
		if (n > 4) {
			this.view_series(second, third, first + second + third, n - 1);
			process.stdout.write("  " + (first + second + third));
		}
	}
	tribonacci(n) {
		this.view_series(0, 1, 1, n);
		if (n > 3) {
			process.stdout.write("  1");
		}
		if (n > 2) {
			process.stdout.write("  1");
		}
		if (n > 1) {
			process.stdout.write("  0");
		}
		if (n > 0) {
			process.stdout.write("  0");
		}
	}
}
function main() {
	var obj = new MyNumber();
	var n = 15;
	obj.tribonacci(n);
}
main();

Output

927  504  274  149  81  44  24  13  7  4  2  1  1  0  0 
/*
  Swift 4 Program
  Reverse tribonacci sequence
*/
class MyNumber {
  func view_series(_ first: Int, _ second: Int, _ third: Int, _ n: Int) {
    if (n > 4) {
      self.view_series(second, third, first + second + third, n - 1);
      print((first + second + third),terminator:"  ");
    }
  }
  func tribonacci(_ n: Int) {
    self.view_series(0, 1, 1, n);
    if (n > 3) {
      print(1,terminator:"  ");
    }
    if (n > 2) {
      print(1,terminator:"  ");
    }
    if (n > 1) {
      print(0,terminator:"  ");
    }
    if (n > 0) {
      print(0,terminator:"  ");
    }

  }
}
  
func main() {
  let obj: MyNumber = MyNumber();
  let n: Int = 15;
  obj.tribonacci(n);
}
main();

Output

927  504  274  149  81  44  24  13  7  4  2  1  1  0  0 
<?php
/*
  Php Program
  Reverse fibonacci sequence
*/

class MyNumber {
  public  function view_series($first, $second, $n) {
    if ($n > 0) {
      $this->view_series($second, $first + $second, $n - 1);
      echo "  ". $first;
    }
  }
  public  function fibonacci($n) {
    $first = 0;
    $second = 1;
    $this->view_series($first, $second, $n);
  }

}

function main() {
  $obj = new MyNumber();
  $n = 20;
  $obj->fibonacci($n);
}
main();

Output

927  504  274  149  81  44  24  13  7  4  2  1  1  0  0 

Time Complexity

The time complexity of the code is O(n), where n is the input parameter. This is because the view_series function is called recursively n times. Each recursive call takes constant time, and the number of recursive calls depends on the value of n.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment