Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
P is for Practical

Re: Golfing a fibonacci number generator

by Codon (Friar)
on Nov 03, 2006 at 21:11 UTC ( #582169=note: print w/ replies, xml ) Need Help??

in reply to Golfing a fibonacci number generator

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:


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

Comment on Re: Golfing a fibonacci number generator
Select or Download Code
Re^2: Golfing a fibonacci number generator
by Anonymous Monk on Nov 03, 2006 at 21:40 UTC
    //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^2: Golfing a fibonacci number generator
by ikegami (Pope) on Nov 03, 2006 at 23:16 UTC

    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

        All your numbers are all concatened together, even with perl -l. I avoided that.

        Anyway, found something shorter still (42):

        The following are my different solutions, all generating the same output under perl -l (except where noted):

        sub{$_[2]=$_[0]+$_[1],print shift for 1..20}->(1,1) #51 @@=(1,1);push@@,$@[-2]+$@[-1]for 1..18;print for@@ #50 @@=(1,1);$@[2]=$@[0]+$@[1],print shift@@for 1..20 #49 @@=(1,0);print$@[@@]=$@[-1]+$@[-2]for 1..20 #43 print$@[@@]=$@[-1]+$@[-2]for(@@=(1,0))..21 #42 Missing first "1": print$@[@@]=$@[-1]+$@[-2]for(@@=1)..19 #38 Loops forever: @@=(1,0);print$@[@@]=$_+$@[-1]for@@ #35 Missing first "1" and loops forever: @@=1;print$@[@@]=$_+$@[-2]for@@ #31

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://582169]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-04-20 04:10 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (485 votes), past polls