Dado el valor de n(n < 10), es decir, número de líneas, imprima el triángulo de Fibonacci.
Ejemplos:
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
Los números de Fibonacci son los números en la siguiente secuencia de enteros.
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
En términos matemáticos, la secuencia Fn de los números de Fibonacci se define por la relación de recurrencia
Fn = Fn-1 + Fn-2
con valores semilla F 1 = 1 y F 2 = 1.
A continuación se muestra la implementación del patrón anterior:
C++
// C++ Implementation for // Fibonacci triangle #include <bits/stdc++.h> using namespace std; // function to fill Fibonacci Numbers // in f[] void fib(int f[], int N) { // 1st and 2nd number of the // series are 1 and 1 f[1] = 1; f[2] = 1; for (int i = 3; i <= N; i++) // Add the previous 2 numbers // in the series and store it f[i] = f[i - 1] + f[i - 2]; } void fiboTriangle(int n) { // Fill Fibonacci numbers in f[] using // fib(). We need N = n*(n+1)/2 Fibonacci // numbers to make a triangle of height // n int N = n * (n + 1) / 2; int f[N + 1]; fib(f, N); // To store next Fibonacci Number to print int fiboNum = 1; // for loop to keep track of // number of lines for (int i = 1; i <= n; i++) { // For loop to keep track of // numbers in each line for (int j = 1; j <= i; j++) cout << f[fiboNum++] << " "; cout << endl; } } // Driver code int main() { int n = 5; fiboTriangle(n); return 0; }
Java
// Java Implementation for // Fibonacci triangle import java.io.*; class GFG { // function to fill Fibonacci Numbers // in f[] static void fib(int f[], int N) { // 1st and 2nd number of the // series are 1 and 1 f[1] = 1; f[2] = 1; for (int i = 3; i <= N; i++) // Add the previous 2 numbers // in the series and store it f[i] = f[i - 1] + f[i - 2]; } static void fiboTriangle(int n) { // Fill Fibonacci numbers in f[] using // fib(). We need N = n*(n+1)/2 Fibonacci // numbers to make a triangle of height // n int N = n * (n + 1) / 2; int f[] = new int[N + 1]; fib(f, N); // To store next Fibonacci // Number to print int fiboNum = 1; // for loop to keep track of // number of lines for (int i = 1; i <= n; i++) { // For loop to keep track of // numbers in each line for (int j = 1; j <= i; j++) System.out.print(f[fiboNum++] + " "); System.out.println(); } } // Driver code public static void main(String args[]) { int n = 5; fiboTriangle(n); } } /*This code is contributed by Nikita Tiwari.*/
Python3
# Python 3 Implementation for # Fibonacci triangle # function to fill Fibonacci # Numbers in f[] def fib(f, N): # 1st and 2nd number of # the series are 1 and 1 f[1] = 1 f[2] = 1 for i in range(3, N + 1): # Add the previous 2 numbers # in the series and store it f[i] = f[i - 1] + f[i - 2] def fiboTriangle(n): # Fill Fibonacci numbers in # f[] using fib(). We need # N = n*(n + 1)/2 Fibonacci # numbers to make a triangle # of height n N = n * (n + 1) // 2 f = [0] * (N + 1) fib(f, N) # To store next Fibonacci # Number to print fiboNum = 1 # for loop to keep track of # number of lines for i in range(1, n + 1): # For loop to keep track of # numbers in each line for j in range(1, i + 1): print(f[fiboNum], " ", end="") fiboNum = fiboNum + 1 print() # Driver code n = 5 fiboTriangle(n) # This code is contributed by Nikita Tiwari.
C#
// C# Implementation for // Fibonacci triangle using System; class GFG { // function to fill Fibonacci Numbers // in f[] static void fib(int[] f, int N) { // 1st and 2nd number of the // series are 1 and 1 f[1] = 1; f[2] = 1; for (int i = 3; i <= N; i++) // Add the previous 2 numbers // in the series and store it f[i] = f[i - 1] + f[i - 2]; } static void fiboTriangle(int n) { // Fill Fibonacci numbers in f[] using // fib(). We need N = n*(n+1)/2 Fibonacci // numbers to make a triangle of height // n int N = n * (n + 1) / 2; int[] f = new int[N + 1]; fib(f, N); // To store next Fibonacci // Number to print int fiboNum = 1; // for loop to keep track of // number of lines for (int i = 1; i <= n; i++) { // For loop to keep track of // numbers in each line for (int j = 1; j <= i; j++) Console.Write(f[fiboNum++] + " "); Console.WriteLine(); } } // Driver code public static void Main() { int n = 5; fiboTriangle(n); } } /*This code is contributed by vt_m.*/
PHP
<?php // PHP Implementation for // Fibonacci triangle // function to fill // Fibonacci Numbers // in f[] function fib(&$f, $N) { // 1st and 2nd number // of the series are // 1 and 1 $f[1] = 1; $f[2] = 1; for ($i = 3; $i <= $N; $i++) // Add the previous // 2 numbers in the // series and store it $f[$i] = $f[$i - 1] + $f[$i - 2]; } function fiboTriangle($n) { // Fill Fibonacci numbers // in f[] using fib(). We // need N = n*(n+1)/2 // Fibonacci numbers to make // a triangle of height n $N = $n * ($n + 1) / 2; $f = array(); fib($f, $N); // To store next // Fibonacci Number // to print $fiboNum = 1; // for loop to keep track // of number of lines for ($i = 1; $i <= $n; $i++) { // For loop to keep track // of numbers in each line for ($j = 1;$j <= $i; $j++) echo ($f[$fiboNum++] . " "); echo("\n"); } } // Driver code $n = 5; fiboTriangle($n); // This code is contributed by // Manish Shaw(manishshaw1) ?>
Javascript
<script> // JavaScript implementation for // Fibonacci triangle // Function to fill Fibonacci Numbers // in f[] function fib(f, N) { // 1st and 2nd number of the // series are 1 and 1 f[1] = 1; f[2] = 1; for(var i = 3; i <= N; i++) // Add the previous 2 numbers // in the series and store it f[i] = f[i - 1] + f[i - 2]; } function fiboTriangle(n) { // Fill Fibonacci numbers in f[] using // fib(). We need N = n*(n+1)/2 Fibonacci // numbers to make a triangle of height // n var N = (n * (n + 1)) / 2; var f = [...Array(N + 1)]; fib(f, N); // To store next Fibonacci Number to print var fiboNum = 1; // for loop to keep track of // number of lines for(var i = 1; i <= n; i++) { // For loop to keep track of // numbers in each line for(var j = 1; j <= i; j++) document.write(f[fiboNum++] + " "); document.write("<br>"); } } // Driver code var n = 5; fiboTriangle(n); // This code is contributed by rdtank </script>
Producción:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
Publicación traducida automáticamente
Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA