Alright, so I've been golfing this one little insignificant fibonacci number generator for a little bit now, and I'm hoping someone on here can point me in a new direction for making it smaller. The first is 4 chars bigger, but is a non-terminating (sane) version. Otherwise, they both do the same thing.
### 73c ### map((print int((($;=((1+($^=5**.5))/2)**$_)-((-1**$_)/$;))/$^).$/),0.. +9); ### 69c ### die map((int((($;=((1+($^=5**.5))/2)**$_)-(-1**$_/$;))/$^).$/),0..9);
Tell me what you think! :0

Replies are listed 'Best First'.
Re: Golfing a fibonacci number generator
by Codon (Friar) on Nov 03, 2006 at 21:11 UTC
    Well, I'm not exactly addressing your math, but I can shave a couple of characters off your map:

    die map{int((($;=((1+($^=5**.5))/2)**$_)-((-1**$_)/$;))/$^).$/}0..9

    By changing the map to use the coderef syntax, you eliminate the need for the comma between the operation and the operands. You can also get rid of that last semicolon in the map block, as it is uneeded.

    My personal fibonacci generator is:

    @@=(1,1);push(@@,$@[0]+$@[1]),print(shift@@)for(1..20)

    Ivan Heffner
    Sr. Software Engineer, DAS Lead
    WhitePages.com, Inc.

      @@=(1,1);push(@@,$@[0]+$@[1]),print(shift@@)for(1..20)
      condenses to
      @@=(1,1);$@[2]=$@[0]+$@[1],print shift@@for 1..20

      For an extra character, you can print all 20 genereated fibs, instead of just 18:
      @@=(1,1);push@@,$@[-2]+$@[-1]for 1..20;print for@@

        let's chop another 6 chars off that: @x=(1);push@x,$x[-2]+$x[-1]for 1..21;print@x
      //Me again// Wow, I like that one alot :) I think that might be the best one I've seen. I figured most people would try to go for the 'add the previous to the next' method, so I put the phi/-phi formula to use.
      Thanks for the help! It's getting close to the lowest it can get, I'd think, because of the necessary bulk of the forumla itself. 67 characters, not bad! :D I appreciate the feedback.

        Since the (-phi)^(-n) term goes to zero quickly, it's enough to round (phi^n)/sqrt(5)to the nearest integer.

        49 characters: die map{int(((1+($^=5**.5))/2)**$_/$^+.5).$/}0..9
Re: Golfing a fibonacci number generator
by McDarren (Abbot) on Nov 04, 2006 at 00:43 UTC
    There is short a section in Advanced Perl Programming (2nd Edition), which covers Perl golfing. The example that the author uses is the Fibonnacci series :)

    It's only a page and a bit, but it's quite good because he explains it along the way. He eventually gets it down to 32 characters :)

    Cheers,
    Darren :)

      Yes, that's right and in realtion to this I posted an year ago the same one-liner but 27 chars long (the shortest one in the world):
      print$}+=$.=$}-$.||1while.1
      Re: Fibonacci numbers (again)


        Doesn't work - at least not for me. My best stab is 39 chars (which I'm sure can be improved):
        ($x,$y)=($y,$x+$y),print$y for$x++..19
Re: Golfing a fibonacci number generator
by ambrus (Abbot) on Nov 04, 2006 at 17:45 UTC