Sponsored Content
Top Forums Shell Programming and Scripting Shell script to generate Fibonacci series using recursion Post 302378787 by jstrangfeld on Tuesday 8th of December 2009 08:27:52 PM
Old 12-08-2009
Wow quite interesting results. Wonder if its some oddity because I am running this in cygwin. I am using a centrino2 vpro2

I changed the 3 scripts to use $1 instead of a read and the times required are vastly different.

Code:
None recursive script I threw together:
$ time ./fibo.sh 10
Fibonacci (10)=55

real    0m0.055s
user    0m0.046s
sys     0m0.030s

Original script from post #2:
$ time ./fibo2.sh 10
0 1 1 2 3 5 8 13 21 34 55

real    0m18.554s
user    0m8.199s
sys     0m4.926s

Script posted by scrutinizer in post #6, well I had to slightly change your while loop cause my shell 
could not digest the inline declaration of "i" so mine looks like this at the bottom of the file:
while [ $i -lt $1 ]
do
let i=i+1
  Fibonacci $i
done
echo

$ time ./fibo3.sh 10
1 1 2 3 5 8 13 21 34 55

real    0m11.508s
user    0m7.953s
sys     0m4.610s

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fibonacci series

Need code to run the Fibonacci series from 0 to 10 (16 Replies)
Discussion started by: nycol
16 Replies

2. UNIX for Dummies Questions & Answers

generate xml from a shell script

Hello! I would like to generate an xml file from the output of various commands generated from within a shell script (some will be in CDATA). At the moment the only solution I have come up with is echoing xml tags around the commands eg. echo "<bitism>" >> outputfile /usr/sbin/prtconf... (1 Reply)
Discussion started by: speedieB
1 Replies

3. Shell Programming and Scripting

How to generate a series of numbers

Hi All, I have a requirement where in I have an input as follows:- input=1-4,6,8-10,12-15 I need to explode this range into an output file as follows:- 1 2 3 4 6 8 9 10 12 13 14 15 My input may vary like 1,5-9,11-13,15-17....... (3 Replies)
Discussion started by: rony_daniel
3 Replies

4. Shell Programming and Scripting

problem in fibonacci series

hi, I'm a beginner to UNIX and got some problem in this fibonacci.Please help me out.Here is the code: fibo() { if then fibo=` expr {fibo ($1 - 2)} + {fibo ($1 - 1)}` | bc echo $fibo fi } echo "enter a number:" read x #echo "The fibonnacci series for value $x is:" fibo $x ... (4 Replies)
Discussion started by: janani_kalyan
4 Replies

5. Shell Programming and Scripting

Shell script to find the sum of first n Fibonacci numbers

pls give me the solution for this i need it for my exam pls pls pls Shell script to find the sum of first n Fibonacci numbers (1 Reply)
Discussion started by: Kshitija
1 Replies

6. Homework & Coursework Questions

Help with shell script to find sum of first n numbers of Fibonacci series

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Shell script to find sum of first n numbers of Fibonacci series 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: Kshitija
0 Replies

7. Shell Programming and Scripting

generate logfile in a shell script

Unix Gurus, I have a shell script which has few "echo" statements. I am trying to create a logfile where all the outputs of the echo statement sare stored. I will have to add this as the final step in the existing script so that everytime the script runs, a logfile is generated with all the... (1 Reply)
Discussion started by: shankar1dada
1 Replies

8. Shell Programming and Scripting

Fibonacci series -going into infinite loop

Hello, I am a beginner to shell programming. Coded the following for Fibonacci series. #!/bin/bash fib() { i=0 j=1 arr=0 arr=1 echo "enter the limit:" read n while do fo= expr $j - 1 f1=$j f2= expr $j + 1 arr= expr ${arr} + ${arr} echo ${arr} (3 Replies)
Discussion started by: Rookie222
3 Replies

9. Shell Programming and Scripting

Generate documentation for a shell script

Hi, I've written a shell script with proper intentation and commenting structure. However, I would like to generate documentation for the shell which I have written. Is there any tool as such to generate it like we have javagen/docgen ? Please help. Thanks, Arjun (0 Replies)
Discussion started by: arjun_arippa
0 Replies

10. UNIX for Beginners Questions & Answers

Shell script newbie- how to generate service log from shell script

Hi, I am totally a newbie to any programming languages and I just started an entry level job in an IT company. One of my recent tasks is to create a script that is able to show the log file of linux service (i.e. ntpd service) lets say, if I run my script ./test.sh, the output should be... (3 Replies)
Discussion started by: xiaogeji
3 Replies
Math::Fibonacci(3pm)					User Contributed Perl Documentation				      Math::Fibonacci(3pm)

NAME
Math::Fibonacci - Fibonacci numbers. VERSION
$Revision: 1.5 $ SYNOPSIS
use Math::Fibonacci qw(term series decompose); my $term = term ( 42 ); my @series = series ( 42 ); my @sum = decompose ( 65535 ); DESCRIPTION
This module provides a few functions related to Fibonacci numbers. EXPORTS ON REQUEST
term(), series() decompose(), isfibonacci() FUNCTIONS
term($n) Returns the $n-th term of the Fibonacci sequence. The term is computed using the fast algorithm: "F(n) ~ g^n/sqrt(5)", where g is the golden ratio and ~ means "take the nearest integer". series($n) Computes and returns the first $n Fibonacci numbers. decompose($int) Decomposes $int into the sum of Fibonacci numbers. Returns the list of Fibonacci numbers. isfibonacci($int) Returns the sequence number of $int if it is a Fibonacci number or a non-true value if it is not. AUTHOR
Vipul Ved Prakash, <mail@vipul.net> LICENSE
Copyright (c) 1999-2001, Vipul Ved Prakash. This code is free software; you can redistribute it and/or modify it under the ARTISTIC license (a copy is included in the distribution) or under the same terms as Perl itself. perl v5.8.8 2008-03-09 Math::Fibonacci(3pm)
All times are GMT -4. The time now is 11:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy