Re: Fibonacci Generator
by Hofmator (Curate) on Aug 10, 2001 at 19:21 UTC
|
if ($opt_c) {
$\ = "\n";
} else {
$\ = " ";
}
at the top of your program (see perlvar). Your prints then simply become print; or print $y;
And two random remarks, you might be interested in this recent thread, also dealing with the Fibonacci series and I hope you developed your program using strict and warnings :)
-- Hofmator
| [reply] [d/l] |
|
Ooooo, I like that. Hadn't even thought of doing it that way. I'm updating the posted code using your suggestion.
I think I developed using strict and whatnot. I almost always do unless it is only going to be a two- or three-liner. Sometimes I still unconsciously rebel against my COBOL days and forget to use strict, but that happens far less often than it used to.
___________________
Kurt
| [reply] |
|
if ($opt_c) {
$\ = "\n";
} else {
$\ = " ";
}
You can avoid that ugly if/else and excess braces by writing:
$/= $opt_c ? "\n" : " ";
| [reply] [d/l] [select] |
Re: Fibonacci Generator
by ariels (Curate) on Aug 12, 2001 at 13:16 UTC
|
All you need in order to generate the next Fibonacci number is the last two; why are you storing all of them? (Also, why not use strict?)
Here's one way; I presented something similar in Fibonacci numbers with lvalues. I give this one as I think it presents an interesting reminder of how "parallel assignment" is useful.
The modification to use Math::BigInt would be easy but obfuscatory for this example.
my ($a,$b)=(1,2); # Start sequence at 1,2 like in code
print "$a\n$b\n";
for(1..$n-2) {
($a,$b) = ($b,$a+$b);
print "$b\n"
}
Save memory, easier to read, etc. | [reply] [d/l] |
|
The reason I was storing all of the number was that originally I thought that I might want to do things with them. Then I decided to scrap that idea and just go with a plain ol' generator. It was a remnant from a previous incarnation. With regard to strict, I always use it if I'm either writing a production script or a script that is going to be more than a few lines long. With something that is really short and isn't ever going to be in serious production use, I only use it about half the time.
I'm posting the modified code as I type this.
___________________
Kurt
| [reply] |
|
| [reply] |
Re: Fibonacci Generator
by PERLscienceman (Curate) on Jul 06, 2003 at 04:27 UTC
|
I was going through the "miscellaneous nodes" section
and found this one of many to be interesting.
So... I thought I would do a little digging and sure
enough, consistent with one of the many glories of
Perl (there is a module for just about everything),
I found one, on CPAN, that generates Fibonacci numbers
and related things.
This is the CPAN documentation URL:
http://search.cpan.org/author/VIPUL/Math-Fibonacci-1.5/lib/Math/Fibonacci.pm
And here is a little chunk of code, (all of 5 lines),
which generates a 10 number Fibonacci Series:
#!/usr/bin/perl -w
use strict;
use Math::Fibonacci qw(series);
my @fibonacci=series(10);
print "@fibonacci\n";
The wonders of Perl! | [reply] [d/l] |
|
could try this out -
if (n==1 || n == 2) {
return 1;}
else {
return (fib(n-1) + fib(n-2));
}
| [reply] |
|
| [reply] |
|
i think this is much better:---
#!/user/bin/perl
$a=0;
$b=1;
for($i=0;$i<=20;$i++)
{
print "$a\n";
$c=$a+$b;
$a=$b;
$b=$c;
}
| [reply] |
Re: Fibonacci Generator
by neosamuri (Friar) on May 27, 2007 at 05:49 UTC
|
use strict;
print join( " ", ratios(fibLikeGen(15,1,1,1))), "\n";
sub fibLikeGen {
my( $len, $n, @seq );
$n = shift;
@seq = @_;
@seq = (1,1) unless @seq;
$len = @seq;
for( my $i = $len; $i < $n; $i++) {
$seq[$i] += $seq[$i - $_] foreach 1..$len;
}
return @seq;
}
sub ratios {
my( @out );
for( my $i = 1; $i < @_; $i++) {
$out[$i] = $_[$i]/$_[$i - 1];
}
return @out;
}
| [reply] [d/l] |