Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Fibonacci Generator

by sifukurt (Hermit)
on Aug 10, 2001 at 18:55 UTC ( [id://103891] : sourcecode . print w/replies, xml ) Need Help??
Category: Miscellaneous
Author/Contact Info Kurt (
Description: Ok, so this is something from the Reinventing the Wheel Department. I was playing and ended up churning out a Fibonacci generator. I thought I'd share it here in the hopes that someone would find it useful. As always, comments welcome.

Update: Made code modifications as suggested.

#Fibonacci Generator
#       Date Written:   22-Jul-2001 10:12:09 AM
#       Last Modified:  13-Aug-2001 10:06:30 AM
#       Author:         Kurt Kincaid
#       This is free software and may be redistributed under the
#       same terms as Perl itself.

use Getopt::Std;
use Math::BigInt;

$| = 1;
$VERSION = "1.2";
$x = Math::BigInt->new(1);
$y = Math::BigInt->new(2);


$\= $opt_c ? "\n" : " ";

if ( $opt_h ) {
    print <<END;
Fibonacci Generator v$VERSION
Usage: $0 [-ch] [number]
-c\tPrint output in a single column (default is rows)
-h\tPrint help text (what you're reading now)

    Include how many numbers in the sequence you want, 
    otherwise it defaults to 100.

my $stop = shift || 100;

print $x;
print $y;
for ( 3 .. $stop ) {
    ( $x, $y ) = ( $y, $x+$y );
    print $y;
Replies are listed 'Best First'.
Re: Fibonacci Generator
by Hofmator (Curate) on Aug 10, 2001 at 19:21 UTC

    You could avoid those ugly ifs before every printout by saying

    if ($opt_c) { $\ = "\n"; } else { $\ = " "; }
    at the top of your program (see perlvar). Your prints then simply become print; or print $y;

    And two random remarks, you might be interested in this recent thread, also dealing with the Fibonacci series and I hope you developed your program using strict and warnings :)

    -- Hofmator

      Ooooo, I like that. Hadn't even thought of doing it that way. I'm updating the posted code using your suggestion.

      I think I developed using strict and whatnot. I almost always do unless it is only going to be a two- or three-liner. Sometimes I still unconsciously rebel against my COBOL days and forget to use strict, but that happens far less often than it used to.
        if ($opt_c) { $\ = "\n"; } else { $\ = " "; }
        You can avoid that ugly if/else and excess braces by writing:
        $/= $opt_c ? "\n" : " ";
Re: Fibonacci Generator
by ariels (Curate) on Aug 12, 2001 at 13:16 UTC
    All you need in order to generate the next Fibonacci number is the last two; why are you storing all of them? (Also, why not use strict?)

    Here's one way; I presented something similar in Fibonacci numbers with lvalues. I give this one as I think it presents an interesting reminder of how "parallel assignment" is useful.

    The modification to use Math::BigInt would be easy but obfuscatory for this example.

    my ($a,$b)=(1,2); # Start sequence at 1,2 like in code print "$a\n$b\n"; for(1..$n-2) { ($a,$b) = ($b,$a+$b); print "$b\n" }
    Save memory, easier to read, etc.
      The reason I was storing all of the number was that originally I thought that I might want to do things with them. Then I decided to scrap that idea and just go with a plain ol' generator. It was a remnant from a previous incarnation. With regard to strict, I always use it if I'm either writing a production script or a script that is going to be more than a few lines long. With something that is really short and isn't ever going to be in serious production use, I only use it about half the time.

      I'm posting the modified code as I type this.
Re: Fibonacci Generator
by PERLscienceman (Curate) on Jul 06, 2003 at 04:27 UTC
    I was going through the "miscellaneous nodes" section and found this one of many to be interesting. So... I thought I would do a little digging and sure enough, consistent with one of the many glories of Perl (there is a module for just about everything), I found one, on CPAN, that generates Fibonacci numbers and related things.
    This is the CPAN documentation URL:
    And here is a little chunk of code, (all of 5 lines), which generates a 10 number Fibonacci Series:
    #!/usr/bin/perl -w use strict; use Math::Fibonacci qw(series); my @fibonacci=series(10); print "@fibonacci\n";

    The wonders of Perl!
      could try this out - if (n==1 || n == 2) { return 1;} else { return (fib(n-1) + fib(n-2)); }
        could try this out - if (n==1 || n == 2) { return 1;} else { return (fib(n-1) + fib(n-2)); }

        Whoa! What algorithm is that? It must be something absolutely new and ingenious: has anybody seen anything similar before?!? But... what language is that?

      i think this is much better:--- #!/user/bin/perl $a=0; $b=1; for($i=0;$i<=20;$i++) { print "$a\n"; $c=$a+$b; $a=$b; $b=$c; }
Re: Fibonacci Generator
by neosamuri (Friar) on May 27, 2007 at 05:49 UTC

    Spurred a thought

    use strict; print join( " ", ratios(fibLikeGen(15,1,1,1))), "\n"; sub fibLikeGen { my( $len, $n, @seq ); $n = shift; @seq = @_; @seq = (1,1) unless @seq; $len = @seq; for( my $i = $len; $i < $n; $i++) { $seq[$i] += $seq[$i - $_] foreach 1..$len; } return @seq; } sub ratios { my( @out ); for( my $i = 1; $i < @_; $i++) { $out[$i] = $_[$i]/$_[$i - 1]; } return @out; }