Scott Hanselman recently posted The Weekly Source 13 – Fibonacci Edition. . It was a very interesting read, but it was missing the Powershell version.

As in the Ruby example, you can use multiple assignments in Powershell as well

Function Get-Fib ($n) {
     $current = $previous = 1;
     while ($current -lt $n) {
           $current;
           $current,$previous = ($current + $previous),$current}
     }
Get-Fib 100

Bruce’s book, Powershell in Action, uses a slightly more terse version of this function in his discussion of multiple variable assignment.

2 Responses to “Fibonacci Series in Powershell”
  1. for the Morris series in PowerShell:

    http://thepowershellguy.com/blogs/posh/archive/2008/01/21/posh-challenge-part-9.aspx

    Greetings /\/\o\/\/

  2. Small correction to the above, correct me if i am wrong..

    $n = Read-Host

    function get-feb ($n) {

    $current = 0 ;
    $previous = 1;
    while ($current -lt $n) {
    $current;
    $current,$previous = ($current+$previous),$current}
    }
    get-feb ($n)

    Fibonacci series starts with 0

Leave a Reply