header image

Fibonacci series

Posted by: | November 23, 2019 Comments Off on Fibonacci series |

Today’s date 23 November is 1123 id written as month then day. Those numbers are the start of the Fibonacci series. So as its Fibonacci day here’s a quick PowerShell function to create a Fibonacci series.


function new-fibonacci {
param (
[int]$numberofelements = 10

## first 2 elements

$ell1 = 1

$ell2 = 1

$i = 2
while ($i -lt $numberofelements){
$elnext = $ell1 + $ell2

$ell2 = $ell1
$ell1 = $elnext




The function has 1 parameter which determines the number of elements of the series you want to produce.


The first two elements are hard coded as being equal to 1 and output from the function.


The bulk of the work is done in the while loop – deliberately chosen to test at the top of the loop.


The next element in the series is the sum of the current last and next to last elements. The counter is incremented, the current last element becomes the next to last and the newly calculated element becomes the current last element.


If you just want the numbers displayed

PS> new-fibonacci


will display the first ten elements

To get a specific number of elements

PS> new-fibonacci -numberofelements 17


If you want the numbers in an array for further processing

PS> $fib = new-fibonacci -numberofelements 17



under: PowerShell

Comments are closed.