The Fibonacci triangle or Hosoya’s triangle is a triangular arrangement of numbers based on Fibonacci numbers. Each number is the sum of two numbers above in either the left diagonal or the right diagonal. The first few rows are:

The numbers in this triangle follow the recurrence relations

Relation to Fibonacci numbers
The entries in the triangle satisfy the identity

Thus, the two outermost diagonals are the Fibonacci numbers, while the numbers on the middle vertical lines are the squares of the Fibonacci numbers. All the other numbers in the triangle are the product of two distinct Fibonacci numbers greater than 1. The row sums are the first convolved Fibonacci numbers.
Sources: Stackoverflow, Wikipedia
Given a positive integers n. The task is print Hosoya’s triangle of size n.
Examples:
Input : n = 4
Output :
1
1 1
2 1 2
3 2 2 3
Input : n = 5
Output :
1
1 1
2 1 2
3 2 2 3
5 3 4 3 5
Below is the implementation of printing Hosoya’s triangle of height n:
C++
#include <bits/stdc++.h>
using namespace std;
int Hosoya( int n, int m)
{
if ((n == 0 && m == 0) ||
(n == 1 && m == 0) ||
(n == 1 && m == 1) ||
(n == 2 && m == 1))
return 1;
if (n > m)
return Hosoya(n - 1, m)
+ Hosoya(n - 2, m);
else if (m == n)
return Hosoya(n - 1, m - 1)
+ Hosoya(n - 2, m - 2);
else
return 0;
}
void printHosoya( int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j <= i; j++)
cout << Hosoya(i, j) << " " ;
cout << endl;
}
}
int main()
{
int n = 5;
printHosoya(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int Hosoya( int n, int m)
{
if ((n == 0 && m == 0 ) ||
(n == 1 && m == 0 ) ||
(n == 1 && m == 1 ) ||
(n == 2 && m == 1 ))
return 1 ;
if (n > m)
return Hosoya(n - 1 , m)
+ Hosoya(n - 2 , m);
else if (m == n)
return Hosoya(n - 1 , m - 1 )
+ Hosoya(n - 2 , m - 2 );
else
return 0 ;
}
static void printHosoya( int n)
{
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j <= i; j++)
System.out.print(Hosoya(i, j)
+ " " );
System.out.println( "" );
}
}
public static void main(String[] args)
{
int n = 5 ;
printHosoya(n);
}
}
|
Python3
def Hosoya( n , m ):
if ((n = = 0 and m = = 0 ) or
(n = = 1 and m = = 0 ) or
(n = = 1 and m = = 1 ) or
(n = = 2 and m = = 1 )):
return 1
if n > m:
return Hosoya(n - 1 , m)
+ Hosoya(n - 2 , m)
elif m = = n:
return Hosoya(n - 1 , m - 1 )
+ Hosoya(n - 2 , m - 2 )
else :
return 0
def printHosoya( n ):
for i in range (n):
for j in range (i + 1 ):
print (Hosoya(i, j) , end = " " )
print ( "\n" , end = "")
n = 5
printHosoya(n)
|
C#
using System;
class GFG {
static int Hosoya( int n, int m)
{
if ((n == 0 && m == 0) ||
(n == 1 && m == 0) ||
(n == 1 && m == 1) ||
(n == 2 && m == 1))
return 1;
if (n > m)
return Hosoya(n - 1, m)
+ Hosoya(n - 2, m);
else if (m == n)
return Hosoya(n - 1, m - 1)
+ Hosoya(n - 2, m - 2);
else
return 0;
}
static void printHosoya( int n)
{
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j <= i; j++)
Console.Write(Hosoya(i, j)
+ " " );
Console.WriteLine( "" );
}
}
public static void Main()
{
int n = 5;
printHosoya(n);
}
}
|
PHP
<?php
function Hosoya(int $n , int $m )
{
if (( $n == 0 && $m == 0) ||
( $n == 1 && $m == 0) ||
( $n == 1 && $m == 1) ||
( $n == 2 && $m == 1))
return 1;
if ( $n > $m )
return Hosoya( $n - 1, $m ) +
Hosoya( $n - 2, $m );
else if ( $m == $n )
return Hosoya( $n - 1, $m - 1) +
Hosoya( $n - 2, $m - 2);
else
return 0;
}
function printHosoya( $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j <= $i ; $j ++)
echo Hosoya( $i , $j ) , " " ;
echo "\n" ;
}
}
$n = 5;
printHosoya( $n );
?>
|
Javascript
<script>
function Hosoya(n, m)
{
if ((n == 0 && m == 0) ||
(n == 1 && m == 0) ||
(n == 1 && m == 1) ||
(n == 2 && m == 1))
return 1;
if (n > m)
return Hosoya(n - 1, m)
+ Hosoya(n - 2, m);
else if (m == n)
return Hosoya(n - 1, m - 1)
+ Hosoya(n - 2, m - 2);
else
return 0;
}
function printHosoya(n)
{
for (let i = 0; i < n; i++)
{
for (let j = 0; j <= i; j++)
document.write(Hosoya(i, j)
+ " " );
document.write( "<br/>" );
}
}
let n = 5;
printHosoya(n);
</script>
|
Output:
1
1 1
2 1 2
3 2 2 3
5 3 4 3 5
Time Complexity: O(n2)
Auxiliary Space: O(n2)
Below is the implementation of printing Hosoya’s triangle of height n using Dynamic Programming:
C++
#include <bits/stdc++.h>
#define N 5
using namespace std;
void printHosoya( int n)
{
int dp[N][N];
memset (dp, 0, sizeof (dp));
dp[0][0] = dp[1][0] = dp[1][1] = 1;
for ( int i = 2; i < n; i++) {
for ( int j = 0; j < n; j++) {
if (i > j)
dp[i][j] = dp[i - 1][j] + dp[i - 2][j];
else
dp[i][j] = dp[i - 1][j - 1] + dp[i - 2][j - 2];
}
}
for ( int i = 0; i < n; i++) {
for ( int j = 0; j <= i; j++)
cout << dp[i][j] << " " ;
cout << endl;
}
}
int main()
{
int n = 5;
printHosoya(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int N = 5 ;
static void printHosoya( int n)
{
int dp[][] = new int [N][N];
dp[ 0 ][ 0 ] = dp[ 1 ][ 0 ] = 1 ;
dp[ 1 ][ 1 ] = 1 ;
for ( int i = 2 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
if (i > j)
dp[i][j] = dp[i - 1 ][j] +
dp[i - 2 ][j];
else
dp[i][j] = dp[i - 1 ][j - 1 ] +
dp[i - 2 ][j - 2 ];
}
}
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j <= i; j++)
System.out.print(dp[i][j] + " " );
System.out.println( "" );
}
}
public static void main(String[] args)
{
int n = 5 ;
printHosoya(n);
}
}
|
Python3
N = 5
def printHosoya(n):
dp = [[ 0 for i in range (N)]
for i in range (N)]
dp[ 0 ][ 0 ] = dp[ 1 ][ 0 ] = dp[ 1 ][ 1 ] = 1
for i in range ( 2 , n):
for j in range (n):
if (i > j):
dp[i][j] = (dp[i - 1 ][j] +
dp[i - 2 ][j])
else :
dp[i][j] = (dp[i - 1 ][j - 1 ] +
dp[i - 2 ][j - 2 ])
for i in range (n):
for j in range (i + 1 ):
print (dp[i][j], end = ' ' )
print ()
n = 5
printHosoya(n)
|
C#
using System;
class GFG {
static int N = 5;
static void printHosoya( int n)
{
int [,]dp = new int [N,N];
dp[0,0] = dp[1,0] = 1;
dp[1,1] = 1;
for ( int i = 2; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if (i > j)
dp[i,j] = dp[i - 1,j] +
dp[i - 2,j];
else
dp[i,j] = dp[i - 1,j - 1]
+ dp[i - 2,j - 2];
}
}
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j <= i; j++)
Console.Write(dp[i,j] + " " );
Console.WriteLine( "" );
}
}
public static void Main()
{
int n = 5;
printHosoya(n);
}
}
|
PHP
<?php
$N =5;
function printHosoya( $n )
{
global $N ;
$dp = array_fill (0, $N , array_fill (0, $N ,0));
$dp [0][0] = $dp [1][0] = $dp [1][1] = 1;
for ( $i = 2; $i < $n ; $i ++) {
for ( $j = 0; $j < $n ; $j ++) {
if ( $i > $j )
$dp [ $i ][ $j ] = $dp [ $i - 1][ $j ]
+ $dp [ $i - 2][ $j ];
else
$dp [ $i ][ $j ] = $dp [ $i - 1][ $j - 1]
+ $dp [ $i - 2][ $j - 2];
}
}
for ( $i = 0; $i < $n ; $i ++) {
for ( $j = 0; $j <= $i ; $j ++)
echo $dp [ $i ][ $j ]. " " ;
echo "\n" ;
}
}
$n = 5;
printHosoya( $n );
?>
|
Javascript
<script>
var N = 5
function printHosoya(n)
{
var dp = Array.from(Array(N),
()=> Array(N).fill(0));
dp[0][0] = dp[1][0] = dp[1][1] = 1;
for ( var i = 2; i < n; i++) {
for ( var j = 0; j < n; j++) {
if (i > j)
dp[i][j] = dp[i - 1][j] +
dp[i - 2][j];
else
dp[i][j] = dp[i - 1][j - 1] +
dp[i - 2][j - 2];
}
}
for ( var i = 0; i < n; i++) {
for ( var j = 0; j <= i; j++)
document.write( dp[i][j] + " " );
document.write( "<br>" );
}
}
var n = 5;
printHosoya(n);
</script>
|
Output:
1
1 1
2 1 2
3 2 2 3
5 3 4 3 5
Time complexity: O(n*n)
space complexity: O(n*n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!