Fibonacci Series in Powershell

January 24, 2008 at 5:01 PMAndy Schneider
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.

Posted in:

Tags:

Comments (2) -

for the Morris series in PowerShell:

thepowershellguy.com/.../...-challenge-part-9.aspx

Greetings /\/\o\/\/

Reply

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

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading