Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Fibonacci Sequence

by tobyink (Abbot)
on Feb 27, 2013 at 08:41 UTC ( #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


Comment on Re: Fibonacci Sequence
Select or Download Code
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 (Prior) 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
Node Status?
node history
Node Type: note [id://1020822]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (18)
As of 2015-07-06 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (75 votes), past polls