Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Fibonacci Sequence

by tobyink (Canon)
on Feb 27, 2013 at 08:41 UTC ( [id://1020822]=note: print w/replies, xml ) Need Help??


in reply to Fibonacci Sequence

Perhaps not especially obscure, but:

$"++,grep{$.=$"+$^,print$.,$/;$^=$",$"=$.}$[..0x10

Update: A variation, less obfuscated, but more golfy if you have a vaguely modern version of Perl...

# 1 2 3 # 12345678901234567890123456789012345678 perl -E'$a=1;map{$.=$a+$b;$b=$a;say$a=$.}0..71'
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^2: Fibonacci Sequence
by skunix (Novice) on Feb 27, 2013 at 14:59 UTC
    Today I learned, $/ :D

      Still not as short as yours but here is mine revised:

      $~=$^++;_:$~^=$^ ^=$~^=$^;$_.=($~=$~+$^.$/);($~<$$)?goto _:print

        Loopy version

        for($\=1;$/<1<<7;$\+=$/,$/=$\-$/){print $"}
Re^2: Fibonacci Sequence
by hdb (Monsignor) on Mar 05, 2013 at 12:01 UTC

    Loopy short version:

    for(++$\;$/<$$;$/=($\+=$/)-$/){print $"}

    Now can someone find a similar short expression for oeis.org/A000040 please?

      $"++;_:while(++$"){$"%$_||next(_)for 2..$"-1;print$",$/}

      Update: trizen has pointed out that this is more efficient:

      $"++;_:while(++$"){$"%$_||next(_)for 2..sqrt$";print$",$/}

      ... but I don't like the two extra keystrokes required. Meanwhile, this solution gives you a minor efficiency boost without any extra nasty keystroke things...

      $"++;_:while(++$"){$"%$_||next(_)for 2..$"/2;print$",$/}
      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-03-29 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found