Reverse tribonacci series

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 


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







© 2021, kalkicode.com, All rights reserved