Re: Fibonacci string
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel)
- Date: 8 Jun 2005 15:48:25 GMT
Ilmari Karonen <usenet@xxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
> Anno Siegel <anno4000@xxxxxxxxxxxxxxxxxxxxxxx> kirjoitti 26.05.2005:
> >
> > Avoiding s/// altogether gains another factor of 50 or so:
> >
> > my $v = 1;
> > for ( 1 .. 20 ) {
> > printf "%10d %10d\n", $v =~ y/0//, $v =~ y/1//;
> > $_ .= length > 1 ? substr( $_, 0, tr/1/1/) : 0 for $v;
> > }
>
> If we're allowed to change the algorithm as long as the same strings
> are produced, I think I can do even better:
I think we should *always* consider a change in algorithm when playing
optimization games. It is the most promising approach, if applicable.
This thread has shown it again.
> my $v = 1; my $v2 = 0;
> for ( 1 .. 20 ) {
> printf "%10d %10d\n", $v =~ y/0//, $v =~ y/1//;
> my $t = $v; $v .= $v2; $v2 = $t;
> }
Ah, an append-only solution. Yes, it beats my fastest version
use constant PHI => ( 1 + sqrt(5))/2;
my $c = 1;
for (1 .. 20) {
$c .= length( $c) > 1 ? substr( $c, 0, int 0.5 + length()/PHI) : 0;
}
slightly.
Anno
.
- Follow-Ups:
- Re: Fibonacci string
- From: Tom Bates
- Re: Fibonacci string
- References:
- Re: Fibonacci string
- From: Ilmari Karonen
- Re: Fibonacci string
- Prev by Date: Re: Is there neq for strings?
- Next by Date: Re: sending file handle as function argument
- Previous by thread: Re: Fibonacci string
- Next by thread: Re: Fibonacci string
- Index(es):
Relevant Pages
|