Imprima la serie de Fibonacci en orden inverso – Part 1

Dado un número n, imprima n términos de la serie de Fibonacci en orden inverso.

Ejemplos: 

Input : n = 5
Output : 3 2 1 1 0

Input : n = 8
Output : 13 8 5 3 2 1 1 0

Algoritmo  

1) Declarar una array de tamaño n. 
2) Inicialice a[0] y a[1] a 0 y 1 respectivamente. 
3) Ejecute un ciclo de 2 a n-1 y almacene la 
suma de a[i-2] y a[i-1] en a[i]
4) Imprima la array en el orden inverso.

C++

// CPP Program to print Fibonacci
// series in reverse order
#include <bits/stdc++.h>
using namespace std;
 
void reverseFibonacci(int n)
{
    int a[n];
 
    // assigning first and second elements
    a[0] = 0;
    a[1] = 1;
 
    for (int i = 2; i < n; i++) {
 
        // storing sum in the
        // preceding location
        a[i] = a[i - 2] + a[i - 1];
    }
 
    for (int i = n - 1; i >= 0; i--) {
 
        // printing array in
        // reverse order
        cout << a[i] << " ";
    }
}
 
// Driver function
int main()
{
    int n = 5;
    reverseFibonacci(n);
    return 0;
}

Java

// Java Program to print Fibonacci
// series in reverse order
import java.io.*;
 
class GFG {
     
    static void reverseFibonacci(int n)
    {
        int a[] = new int[n];
     
        // assigning first and second elements
        a[0] = 0;
        a[1] = 1;
     
        for (int i = 2; i < n; i++)
        {
     
            // storing sum in the
            // preceding location
            a[i] = a[i - 2] + a[i - 1];
        }
     
        for (int i = n - 1; i >= 0; i--)
        {
     
            // printing array in
            // reverse order
            System.out.print(a[i] +" ");
        }
    }
     
    // Driver function
    public static void main(String[] args)
    {
        int n = 5;
        reverseFibonacci(n);
     
    }
}
 
// This code is contributed by vt_m.

Python3

# Python 3 Program to print Fibonacci
# series in reverse order
 
def reverseFibonacci(n):
  
    a = [0] * n
 
    # assigning first and second elements
    a[0] = 0
    a[1] = 1
 
    for i in range(2, n): 
 
        # storing sum in the
        # preceding location
        a[i] = a[i - 2] + a[i - 1]
      
 
    for i in range(n - 1, -1 , -1): 
 
        # printing array in
        # reverse order
        print(a[i],end=" ")
      
  
 
# Driver function
n = 5
reverseFibonacci(n)

C#

// C# Program to print Fibonacci
// series in reverse order
using System;
 
class GFG {
     
    static void reverseFibonacci(int n)
    {
        int []a = new int[n];
     
        // assigning first and second elements
        a[0] = 0;
        a[1] = 1;
     
        for (int i = 2; i < n; i++)
        {
     
            // storing sum in the
            // preceding location
            a[i] = a[i - 2] + a[i - 1];
        }
     
        for (int i = n - 1; i >= 0; i--)
        {
     
            // printing array in
            // reverse order
            Console.Write(a[i] +" ");
        }
    }
     
    // Driver function
    public static void Main()
    {
        int n = 5;
        reverseFibonacci(n);
     
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP Program to print Fibonacci
// series in reverse order
 
function reverseFibonacci($n)
{
 
    // assigning first and
    // second elements
    $a[0] = 0;
    $a[1] = 1;
 
    for ($i = 2; $i < $n; $i++)
    {
 
        // storing sum in the
        // preceding location
        $a[$i] = $a[$i - 2] +
                 $a[$i - 1];
    }
 
    for ($i = $n - 1; $i >= 0; $i--)
    {
 
        // printing array in
        // reverse order
        echo($a[$i] . " ");
    }
}
 
// Driver COde
$n = 5;
reverseFibonacci($n);
 
// This code is contributed by Ajit.
?>

Javascript

<script>
 
// JavaScript program to print Fibonacci
// series in reverse order
function reverseFibonacci(n)
{
    let a = [];
     
    // Assigning first and second elements
    a[0] = 0;
    a[1] = 1;
   
    for(let i = 2; i < n; i++)
    {
         
        // Storing sum in the
        // preceding location
        a[i] = a[i - 2] + a[i - 1];
    }
   
    for(let i = n - 1; i >= 0; i--)
    {
         
        // Printing array in
        // reverse order
        document.write(a[i] + " ");
    }
}
 
// Driver Code
let n = 5;
 
reverseFibonacci(n);
 
// This code is contributed by avijitmondal1998
 
</script>

Producción: 

3 2 1 1 0

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(n)

Publicación traducida automáticamente

Artículo escrito por nickhilrawat y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *