Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

fibo's 66 chars

by Oromis92 (Sexton)
on May 24, 2009 at 11:20 UTC ( [id://765916]=obfuscated: print w/replies, xml ) Need Help??

$a=shift;@b=(0,1);push@b,$b[-2]+$b[-1]while$#b<$a;print"$b[-2]\n";

Replies are listed 'Best First'.
Re: fibo's 66 chars
by TimToady (Parson) on May 28, 2009 at 04:23 UTC
    Not much point in golfing fibonacci in Perl 6:
    0,1...&[+]
    And it's almost readable...
      Really?! You're making that up! :D
Re: fibo's 66 chars
by pepik_knize (Scribe) on May 26, 2009 at 05:28 UTC
    I agree, very nice. If you're golfing this, I have a few reductions for you:

    You can save yourself a couple of characters by using pop instead of shift.

    Also, if you use -l, you can get rid of the quotes and newline:
    $a=pop;@b=(0,1);push@b,$b[-2]+$b[-1]while$#b<$a;print$b[-2]

    Then, you can cut down the while$#b<$a by using for(2..$a):
    $a=pop;@b=(0,1);push@b,$b[-2]+$b[-1]for(2..$a);print$b[-2]

    Of all the causes that conspire to blind
    Man's erring judgment, and misguide the mind,
    What the weak head with strongest bias rules,
    Is pride, the never-failing vice of fools.
    -- Pope.
      Since you only use the parameter once, it's cheaper to just reference it as $ARGV[0] pop it inline rather than assign it to anything.
      @b=(0,1);push@b,$b[-2]+$b[-1]for 2..pop;print$b[-2]
      You can save fourteen more strokes (and, irrelevantly, memory) by not using an array and using convoluted scalar assignment with a variable that is pre-initialized:
      $b=1;$?=($b+=$?)-$?for 2..pop;print$?

      Caution: Contents may have been coded under pressure.
Re: fibo's 66 chars
by CountZero (Bishop) on May 24, 2009 at 15:04 UTC
    Very nice! ++

    And for a change, without using recursion.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: fibo's 66 chars
by ambrus (Abbot) on May 27, 2009 at 19:31 UTC
Re: fibo's 66 chars
by Anonymous Monk on May 27, 2009 at 16:06 UTC
    Or, without creating a large list:
    @b=(0,1);@b=($b[1],$b[0]+$b[1])for(2..pop);print$b[1];
    (and there is no reason to assign $a)

    20090727 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

      I starting going down that path too, but noticed that it fails for 1.
      $ perl -e'@b=(0,1);@b=($b[1],$b[0]+$b[1])for(2..pop);print$b[1]' 1 1

      Of all the causes that conspire to blind
      Man's erring judgment, and misguide the mind,
      What the weak head with strongest bias rules,
      Is pride, the never-failing vice of fools.
      -- Pope.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-12-08 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.