Posted on by Kalkicode
Code Number

# Print Tribonacci Numbers

In mathematics, the Tribonacci sequence is an extension of the Fibonacci sequence, where each number is the sum of the three preceding numbers. The Tribonacci sequence starts with three initial values, and each subsequent number is the sum of the three previous numbers. In this article, we will discuss how to print the first 'n' Tribonacci numbers using a program.

## Problem Statement

Write a program to print the first 'n' Tribonacci numbers, given 'n' as input.

## Example

Let's take 'n = 15' as an example. The first 15 Tribonacci numbers would be: 0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927

## Pseudocode

``````// Function to print the first 'n' Tribonacci numbers
tribonacci(n):
// Set the initial values of Tribonacci sequence
first = 0
second = 1
third = 1

// Variable to store the current Tribonacci number
result = 0

// Base cases (print initial values)
if n > 0:
print 0
if n > 1:
print 0
if n > 2:
print 1
if n > 3:
print 1

// Decrement 'n' by 4 since we have already printed the first four numbers
n = n - 4

// Loop to calculate and print the Tribonacci numbers
while n > 0:
// Calculate the current Tribonacci number
result = first + second + third

// Print the current Tribonacci number
print result

// Update 'first', 'second', and 'third' for the next iteration
first = second
second = third
third = result

// Decrement 'n' by 1
n = n - 1

// Main function
main():
// Input the value of 'n' (the number of Tribonacci numbers to be printed)
n = 15

// Call the 'tribonacci' function to print the Tribonacci numbers
tribonacci(n)

// End of the program
``````
1. Start by defining the function 'tribonacci' that takes an integer 'n' as input.
2. Initialize three variables 'first', 'second', and 'third' with the initial Tribonacci values: 0, 1, and 1, respectively.
3. Initialize a variable 'result' to store the current Tribonacci number.
4. Check for base cases: a. If 'n' is greater than 0, print 0. b. If 'n' is greater than 1, print 0. c. If 'n' is greater than 2, print 1. d. If 'n' is greater than 3, print 1. (These are the initial Tribonacci values.)
5. Decrement 'n' by 4 since we have already printed the first four numbers.
6. Enter a loop that runs while 'n' is greater than 0: a. Calculate the current Tribonacci number by adding 'first', 'second', and 'third'. b. Print the current Tribonacci number. c. Update 'first', 'second', and 'third' for the next iteration. d. Decrement 'n' by 1.
7. End the function 'tribonacci'.

## Algorithm Explanation

The Tribonacci sequence is calculated iteratively by adding the last three numbers to get the next number. We start by printing the first four numbers (0, 0, 1, 1) as they are the initial values. Then, we enter a loop to calculate and print the subsequent Tribonacci numbers. Each number depends on the three previous numbers, and the loop runs until 'n' becomes zero.

## Code Solution

Here given code implementation process.

``````//C Program
//Tribonacci Numbers
#include <stdio.h>

void tribonacci(int n)
{
//Set the initial value of variable
//This is Three initial value
int first  = 0;
int second = 1;
int third  = 1;

int result = 0;

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

while(n > 0)
{

result = first + second + third;

printf("  %d",result);

first  = second;

second = third;

third  = result;

n--;
}

}

int main()
{

int n = 15;

tribonacci(n);

return 0;
}```
```

#### Output

``0  0  1  1  2  4  7  13  24  44  81  149  274  504  927``
``````/*
C++ Program
Tribonacci Numbers
*/

#include<iostream>

using namespace std;

class Number {
public:
void tribonacci(int n) {
//Set the initial value of variable
//This is Three initial value
int first = 0;
int second = 1;
int third = 1;
int result = 0;
if (n > 0) {
cout << ("  0");
}
if (n > 1) {
cout << ("  0");
}
if (n > 2) {
cout << ("  1");
}
if (n > 3) {
cout << ("  1");
}
n -= 4;
while (n > 0) {
result = first + second + third;
cout << "  " << result;
first = second;
second = third;
third = result;
n--;
}
}

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

#### Output

``  0  0  1  1  2  4  7  13  24  44  81  149  274  504  927``
``````/*
Java Program
Tribonacci Numbers
*/
public class Number {

public void tribonacci(int n) {
//Set the initial value of variable
//This is Three initial value
int first = 0;
int second = 1;
int third = 1;

int result = 0;

//Base cases
if (n > 0) {
System.out.print("  0");
}
if (n > 1) {
System.out.print("  0");
}
if (n > 2) {
System.out.print("  1");
}
if (n > 3) {
System.out.print("  1");
}
n -= 4;

while (n > 0) {

result = first + second + third;

System.out.print("  " + result);

first = second;

second = third;

third = result;

n--;
}

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

int n = 15;

obj.tribonacci(n);

}
}```
```

#### Output

``  0  0  1  1  2  4  7  13  24  44  81  149  274  504  927``
``````#Python3 Program
#Tribonacci Numbers
class Number :
def tribonacci(self, n) :

#Set the initial value of variable
#This is Three initial value
first = 0
second = 1
third = 1
result = 0
if (n > 0) :
print(0,end=" ")

if (n > 1) :
print(0,end=" ")

if (n > 2) :
print(1,end=" ")

if (n > 3) :
print(1,end=" ")

n -= 4
while (n > 0) :
result = first + second + third
print(result,end=" ")
first = second
second = third
third = result
n -= 1

def main() :
obj = Number()
n = 15
obj.tribonacci(n)

if __name__ == "__main__":
main()```
```

#### Output

``0 0 1 1 2 4 7 13 24 44 81 149 274 504 927``
``````/*
C# Program
Tribonacci Numbers
*/
using System;
public class Number {

public void tribonacci(int n)
{
//Set the initial value of variable
//This is Three initial value
int first  = 0;
int second = 1;
int third  = 1;

int result = 0;

//Base cases
if(n > 0)
{
Console.Write("  0");
}
if(n > 1)
{
Console.Write("  0");
}
if(n > 2)
{
Console.Write("  1");
}
if(n > 3)
{
Console.Write("  1");
}
n-=4;

while(n > 0)
{

result = first + second + third;

Console.Write("  "+result);

first  = second;

second = third;

third  = result;

n--;
}

}
public static void Main(String[] args) {
Number obj = new Number();

int n = 15;

obj.tribonacci(n);

}
}```
```

#### Output

``````0  0  1  1  2  4  7  13  24  44  81  149  274  504  927
``````
``````#Ruby Program
#Tribonacci Numbers
class Number
def tribonacci(n)
first = 0
second = 1
third = 1
result = 0
if (n > 0)
print("  0")
end
if (n > 1)
print("  0")
end
if (n > 2)
print("  1")
end
if (n > 3)
print("  1")
end
n -= 4
while (n > 0)
result = first + second + third
print("  ", result)
first = second
second = third
third = result
n -= 1
end
end
end
def main()
obj = Number.new()
n = 15
obj.tribonacci(n)
end
main()
```
```

#### Output

`` 0  0  1  1  2  4  7  13  24  44  81  149  274  504  927``
``````/*
Node Js Program
Tribonacci Numbers
*/
class Number {
tribonacci(n) {
var first = 0;
var second = 1;
var third = 1;
var result = 0;
if (n > 0) {
process.stdout.write("  0");
}
if (n > 1) {
process.stdout.write("  0");
}
if (n > 2) {
process.stdout.write("  1");
}
if (n > 3) {
process.stdout.write("  1");
}
n -= 4;
while (n > 0) {
result = first + second + third;
process.stdout.write("  " + result);
first = second;
second = third;
third = result;
n--;
}
}
}

function main() {
var obj = new Number();
var n = 15;
obj.tribonacci(n);
}
main();```
```

#### Output

`` 0  0  1  1  2  4  7  13  24  44  81  149  274  504  927``
``````/*
Swift 4
Tribonacci Numbers
*/
class Number {
func tribonacci(_ n: Int) {
var first: Int = 0;
var second: Int = 1;
var third: Int = 1;
var result: Int = 0;
var num = n;
if (n > 0) {
print(0,terminator : "  ");
}
if (n > 1) {
print(0,terminator : "  ");
}
if (n > 2) {
print(1,terminator : "  ");
}
if (n > 3) {

print(1,terminator : "  ");
}
num = n - 4;
while (num > 0) {
result = first + second + third;
print(result,terminator : "  ");
first = second;
second = third;
third = result;
num -= 1;
}
}

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

#### Output

`` 0  0  1  1  2  4  7  13  24  44  81  149  274  504  927``
``````#   Python 3 Program
#   Tribonacci Numbers

class Number :
def tribonacci(self, n) :
# Set the initial value of variable
# This is Three initial value
first = 0
second = 1
third = 1
result = 0
# Base cases

if (n > 0) :
print(" 0", end = " ")

if (n > 1) :
print(" 0", end = " ")

if (n > 2) :
print(" 1", end = " ")

if (n > 3) :
print(" 1", end = "")

n -= 4
while (n > 0) :
result = first + second + third
print(" ", result, end = " ")
first = second
second = third
third = result
n -= 1

def main() :
obj = Number()
n = 15
obj.tribonacci(n)

if __name__ == "__main__":
main()```
```

#### Output

`` 0  0  1  1  2   4   7   13   24   44   81   149   274   504   927``
``````/*
Scala Program
Tribonacci Numbers
*/
class Number {
def tribonacci(value: Int): Unit = {
//Set the initial value of variable
//This is Three initial value
var first: Int = 0;
var second: Int = 1;
var third: Int = 1;
var result: Int = 0;

//Base cases

if (value > 0) {
print(" 0");
}
if (value > 1) {
print(" 0");
}
if (value > 2) {
print(" 1");
}
if (value > 3) {
print(" 1");
}
var n : Int = value-4;
while (n > 0) {
result = first + second + third;
print(" " + result);
first = second;
second = third;
third = result;
n -= 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: Number = new Number();
var n: Int = 15;
obj.tribonacci(n);
}
}```
```

#### Output

`` 0 0 1 1 2 4 7 13 24 44 81 149 274 504 927``
``````fn main(){
let n: i32 = 15;
tribonacci(n);
}
fn tribonacci(n: i32) {
//Set the initial value of variable
//This is Three initial value
let mut first: i32 = 0;
let mut second: i32 = 1;
let mut third: i32 = 1;
let mut result: i32 ;
//Base cases

if n > 0 {
print!(" {}", 0);
}
if n > 1 {
print!(" {}", 0);
}
if n > 2 {
print!(" {}", 1);
}
if n > 3 {
print!(" {}", 1);
}
let mut value = n - 4;
while value > 0 {
result = first + second + third;
print!(" {}", result);
first = second;
second = third;
third = result;
value-= 1;
}
}```
```

#### Output

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

## Time Complexity

The time complexity of this program is O(n), where 'n' is the number of Tribonacci numbers to be printed. Since we iterate through the loop 'n' times, the time complexity is linearly proportional to 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.

Categories
Relative Post