in reply to Fibonacci numbers

Here's the most simple program to do it. You can test it.
$a=0; $b=1; while($a <=200) {print("$a\n"); ($a,$b)=($b,$a+$b); }

Replies are listed 'Best First'.
Re: Re: Fibonacci numbers(Again)
by Anonymous Monk on Oct 02, 2001 at 23:48 UTC
    How does this statement
    ($a,$b)=($a,$a+$b);
    does the trick? I couldn't understand...can somebody explain?

      Perhaps you can't see it because you've got the statement wrong. It should be

      ($a,$b)=($b,$a+$b);

      It's a simple list assignment. $a gets the value of $b and $b gets $a+$b.

      Incidently, it can be a bad idea to use $a and $b in Perl scripts as they are "special" variables because of their use in sorting.

      Blessed Be
      The Pixel

      Dear Anonymous, It's very simple. In every iteration the $a receives the earlier value of $b...that's the trick. Bye the way, u can see my ealier attempt, which had one serious mistake... that can be rewritten to give the desired result. See:
      $a=1; $b=1; while($a <=200) {$c=$a+$b; print("$a\n$b\n$c\n"); $a=$b+$c; $b=$c+$a; }
      Thanks.

      Oh, for goodness sake. This is homework, right?

      For the answer to this latest question, see the first answer in the thread...

      (Alternatively, read what you're quoting: it might make more sense then.)