Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Fibonacci numbers (again)

by sh1tn (Priest)
on Sep 08, 2005 at 15:39 UTC ( #490241=obfuscated: print w/replies, xml ) Need Help??

in 30 chars:
perl -e '$.++;print$}+=$.,$.+=$}while+1'


Replies are listed 'Best First'.
Re: Fibonacci numbers (again)
by sh1tn (Priest) on Sep 08, 2005 at 23:00 UTC
    The same in 28 chars:
    print$}+=$.||1,$.+=$}while.1
    Update: The same in 27 chars:
    print$}+=$.=$}-$.||1while.1


      it would nice to explain the code ... Murcia
        print$}+=$.=$}-$.||1while.1 # means - while ( 1 ) { $. = $} - $.; $. ||= 1; $} = $} + $.; print $}; }


Re: Fibonacci numbers (again)
by cristian (Hermit) on Sep 08, 2005 at 17:24 UTC
    Updated In 28 chars
    {print$}+=$.||1,$.+=$};redo}
      Please, recheck your code. It should output these numbers:
      1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 ... i.e.: perl -e '$.++;print$}+=$.,q| |,$.+=$},q| |while+1'|more


        Which part are you criticizing? I thought fibo numbers where 1,1,2,3,5,8 which would make his correct. Just asking for clarification if there was some other difference i missed.

        His outputs 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597 for me.

        Update: /me does some googling and finds both of those and 0,1,1,2,3 as pretty evenly distributed....hmm.


        ___________
        Eric Hodges
Re: Fibonacci numbers (again)
by ambrus (Abbot) on Sep 09, 2005 at 12:01 UTC

    This is even shorter, but it prints newlines, not spaces. It's better than the perl one because it calculates all digits.

    dc -e1d[pdsd+ldrlxx]dsxx
    If it's enough to have the first say 40 elements of the sequence (depending on an internal limitation of dc) then this is 2 chars shorter:
    dc -e1d[pdk+Krlxx]dsxx

    If you insist on spaces, here's a version:

    dc -e1d[ddn32Psd+ldrlxx]dsxx

    These depend on the dc extension command r (and the last one on n and P too), but these are present in both GNU dc and FreeBSD dc, so it might be portable now.

    Update 2006 nov 15:

    This is slightly longer than the above but is still interesting:

    dc -e'1[pdd5**v1++2/lxx]dsxx'

    Update 2007 jul 17: some explanation in Re^3: Fibo Golf challenge on 3 monkeys.

Re: Fibonacci numbers (again)
by liverpole (Monsignor) on Sep 08, 2005 at 16:43 UTC
    Excellent ++ very succinct!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://490241]
Approved by marto
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2022-05-16 15:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (63 votes). Check out past polls.

    Notices?