Hey, Scripting Guy! Weekend Scripter: Fibonnaci Sequence This!


Bookmark and Share

Microsoft Scripting Guy Ed Wilson here, it is warming up a bit down here in Charlotte, North Carolina in the United States, and therefore I tend to gravitate outside on weekends. At least, far enough outside to make it to my woodworking shop. Today, I was thinking about building a series of boxes. One of the golden rules in box design is the so-called golden mean. An off shot of the golden mean, are Fibonacci numbers where the next number in a series is made of the two previous numbers. The numbers would look like this:

1,2,3,5,8,13,21,34,55,89,144 …

What does this series of numbers have to do with woodworking? You can use those numbers as proportions when making a series of stackable boxes, drawers for a chest of drawers, or a set of shelves.

An example of how these numbers would look when stacked is seen in the following image.

Image of Fibonnaci numbers stacked 

There are functions available on-line that illustrate creating Fibonacci numbers in many different languages. In fact, the Get-PowerShell web site even has one. I decided to write one myself … admittedly similar to the one that Andy wrote, but still a bit different. One of the cool things about my function is the use of multiple variable assignment in the For statement. This is seen here.

For($i = $j = 1; $i -lt $max)

Another thing that is cool, is the ability to add multiple values to variables on the same line. This is seen here.

$i,$j = ($i + $j),$i

The Get-Fibonacci.ps1 script is seen here.


Function Get-Fibonacci
  For($i = $j = 1; $i -lt $max)
    $i,$j = ($i + $j),$i
   } #end for
} #end function

When the function runs, it produces Fibonacci numbers up to the value you supply for $max. This is seen in the following image.

Image of output when script runs


Fibonacci numbers are fun, and provide their own enjoyment. The ability of Windows PowerShell to allow for multiple value assignments in the for statement, and to assign multiple values to variables inside the loop makes the Windows PowerShell coding requirements much easier. This concludes weekend scripter for this weekend.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.


Ed Wilson and Craig Liebendorfer, Scripting Guys



Discussion is closed.

Feedback usabilla icon