Given the value of n(n < 10), i.e, number of lines, print the Fibonacci triangle.
Examples:
Input : n = 5
Output :
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
Input : n = 7
Output :
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121393 196418 317811
The Fibonacci numbers are the numbers in the following integer sequence.
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
Fn = Fn-1 + Fn-2
with seed values F1 = 1 and F2 = 1.
Below is the implementation of the above pattern :
C++
#include <bits/stdc++.h>
using namespace std;
void fib( int f[], int N)
{
f[1] = 1;
f[2] = 1;
for ( int i = 3; i <= N; i++)
f[i] = f[i - 1] + f[i - 2];
}
void fiboTriangle( int n)
{
int N = n * (n + 1) / 2;
int f[N + 1];
fib(f, N);
int fiboNum = 1;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= i; j++)
cout << f[fiboNum++] << " " ;
cout << endl;
}
}
int main()
{
int n = 5;
fiboTriangle(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void fib( int f[], int N)
{
f[ 1 ] = 1 ;
f[ 2 ] = 1 ;
for ( int i = 3 ; i <= N; i++)
f[i] = f[i - 1 ] + f[i - 2 ];
}
static void fiboTriangle( int n)
{
int N = n * (n + 1 ) / 2 ;
int f[] = new int [N + 1 ];
fib(f, N);
int fiboNum = 1 ;
for ( int i = 1 ; i <= n; i++) {
for ( int j = 1 ; j <= i; j++)
System.out.print(f[fiboNum++] + " " );
System.out.println();
}
}
public static void main(String args[])
{
int n = 5 ;
fiboTriangle(n);
}
}
|
Python3
def fib(f, N):
f[ 1 ] = 1
f[ 2 ] = 1
for i in range ( 3 , N + 1 ):
f[i] = f[i - 1 ] + f[i - 2 ]
def fiboTriangle(n):
N = n * (n + 1 ) / / 2
f = [ 0 ] * (N + 1 )
fib(f, N)
fiboNum = 1
for i in range ( 1 , n + 1 ):
for j in range ( 1 , i + 1 ):
print (f[fiboNum], " " , end = "")
fiboNum = fiboNum + 1
print ()
n = 5
fiboTriangle(n)
|
C#
using System;
class GFG {
static void fib( int [] f, int N)
{
f[1] = 1;
f[2] = 1;
for ( int i = 3; i <= N; i++)
f[i] = f[i - 1] + f[i - 2];
}
static void fiboTriangle( int n)
{
int N = n * (n + 1) / 2;
int [] f = new int [N + 1];
fib(f, N);
int fiboNum = 1;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= i; j++)
Console.Write(f[fiboNum++] + " " );
Console.WriteLine();
}
}
public static void Main()
{
int n = 5;
fiboTriangle(n);
}
}
|
PHP
<?php
function fib(& $f , $N )
{
$f [1] = 1;
$f [2] = 1;
for ( $i = 3;
$i <= $N ; $i ++)
$f [ $i ] = $f [ $i - 1] +
$f [ $i - 2];
}
function fiboTriangle( $n )
{
$N = $n * ( $n + 1) / 2;
$f = array ();
fib( $f , $N );
$fiboNum = 1;
for ( $i = 1; $i <= $n ; $i ++)
{
for ( $j = 1; $j <= $i ; $j ++)
echo ( $f [ $fiboNum ++] . " " );
echo ( "\n" );
}
}
$n = 5;
fiboTriangle( $n );
?>
|
Javascript
<script>
function fib(f, N)
{
f[1] = 1;
f[2] = 1;
for ( var i = 3; i <= N; i++)
f[i] = f[i - 1] + f[i - 2];
}
function fiboTriangle(n)
{
var N = (n * (n + 1)) / 2;
var f = [...Array(N + 1)];
fib(f, N);
var fiboNum = 1;
for ( var i = 1; i <= n; i++)
{
for ( var j = 1; j <= i; j++)
document.write(f[fiboNum++] + " " );
document.write( "<br>" );
}
}
var n = 5;
fiboTriangle(n);
</script>
|
Output:
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
Time complexity: O(n*n)
Auxiliary Space: O(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!
Last Updated :
10 Aug, 2022
Like Article
Save Article