CODE | Fibonacci em Shell Script

Bom dia a todos,
Hoje trago-vos duas formas de calcular a sequência de números de Fibonacci atravez de sheel script.

Por definição matemática, os números de Fibonacci são os números na sequência abaixo apresentada:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

Por definição, os primeiros dois números de Fibonacci são o 0 e 1,e cada número subsequente é a soma dos dois anteriores, Em termos matemáticos, a sequência de números de Fibonacci Fn é definida pela relação recursiva:

Fn = Fn-1 + Fn-2, com os valores semente F0 = 0 e F1 = 1

Modo Iterativo

#!/bin/bash
################################################################################
# SCRIPT: fibonacci_iterative.sh
# USAGE: fibonacci_iterative.sh [Number]
# PURPOSE: Generate Fibonacci sequence.
#
################################################################################
# Verifica se existe um argumento de entrada, se não pede um numero
################################################################################

if [ $# -eq 1 ]
then
 Num=$1
else
 printf "Enter a Number: "
 read Num
fi

################################################################################
# O ponto principal do script começa aqui
################################################################################
f1=0
f2=1

echo "The Fibonacci sequence for the number $Num is : "

for (( i=0;i<=Num;i++ ))
do
 printf "%d " "$f1"
 fn=$((f1+f2))
 f1=$f2
 f2=$fn
done

echo

Método Recursivo

#!/bin/bash
################################################################################
# SCRIPT:   fibonacci_recursive.sh
# USAGE:   fibonacci_recursive.sh [Number]
# PURPOSE: Generate Fibonacci sequence.
#
################################################################################
#  Verifica se existe um argumento de entrada, se não pede um numero
################################################################################

if [ $# -eq 1 ]
then
    Num=$1
else
    printf  "Enter a Number: "
    read Num
fi

################################################################################
# Define a função que vai ser usada para calcular a sequencia de fibonacci
################################################################################
Fibonacci()
{
case $1 in
 0|1)
	printf "%d " "$1"
	;;

*)
	printf "$(( $(Fibonacci $(($1-2)))+$(Fibonacci $(($1-1))) )) "
	;;
esac
}

################################################################################
# O ponto principal do script começa aqui
################################################################################

echo "The Fibonacci sequence for the number $Num is : "

for (( i=0; i<=$Num; i++ ))
do
  Fibonacci $i	# Chama a função Fibonacci
done

echo

Aqui fica o tempo necessário para cada um algoritmos.

new-host:Desktop gilmendes$ sudo time sh fibonacci_iterative.sh 15
The Fibonacci sequence for the number 15 is :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
0.00 real 0.00 user 0.00 sys
new-host:Desktop gilmendes$ sudo time sh fibonacci_recursive.sh 15
The Fibonacci sequence for the number 15 is :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
3.33 real 1.22 user 1.95 sys

Deixe uma Resposta

Preencha os seus detalhes abaixo ou clique num ícone para iniciar sessão:

Logótipo da WordPress.com

Está a comentar usando a sua conta WordPress.com Terminar Sessão / Alterar )

Imagem do Twitter

Está a comentar usando a sua conta Twitter Terminar Sessão / Alterar )

Facebook photo

Está a comentar usando a sua conta Facebook Terminar Sessão / Alterar )

Google+ photo

Está a comentar usando a sua conta Google+ Terminar Sessão / Alterar )

Connecting to %s