PowerShell Script to Print Fibonacci Series
Use a loop in PowerShell to generate Fibonacci numbers like this:
$a=0; $b=1; for ($i=0; $i -lt $n; $i++) { Write-Output $a; $temp=$a+$b; $a=$b; $b=$temp } where $n is the count of numbers to print.Examples
Input5
Output0
1
1
2
3
Input1
Output0
Input0
Output
How to Think About It
To print the Fibonacci series, start with the first two numbers 0 and 1. Then repeatedly add the last two numbers to get the next one. Print each number as you go until you reach the desired count.
Algorithm
1
Get the number of Fibonacci numbers to print (n).2
Initialize two variables with 0 and 1.3
Loop from 0 to n-1.4
Print the current first number.5
Calculate the next number by adding the two variables.6
Update the variables to move forward in the series.Code
powershell
$n = 10 $a = 0 $b = 1 for ($i = 0; $i -lt $n; $i++) { Write-Output $a $temp = $a + $b $a = $b $b = $temp }
Output
0
1
1
2
3
5
8
13
21
34
Dry Run
Let's trace printing first 5 Fibonacci numbers through the code
1
Initialize variables
$a=0, $b=1, $n=5
2
First iteration
Print $a=0; calculate temp=0+1=1; update $a=1, $b=1
3
Second iteration
Print $a=1; calculate temp=1+1=2; update $a=1, $b=2
| Iteration | Temp | a after update | b after update | |
|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 1 |
| 1 | 1 | 2 | 1 | 2 |
| 2 | 1 | 3 | 2 | 3 |
| 3 | 2 | 5 | 3 | 5 |
| 4 | 3 | 8 | 5 | 8 |
Why This Works
Step 1: Start with base numbers
The Fibonacci series begins with 0 and 1, so we set $a=0 and $b=1.
Step 2: Print current number
In each loop, we print the current value of $a which holds the current Fibonacci number.
Step 3: Calculate next number
We add $a and $b to get the next Fibonacci number, then update $a and $b to move forward.
Alternative Approaches
Recursive function
powershell
function Get-Fibonacci($n) { if ($n -le 1) { return $n } return (Get-Fibonacci ($n - 1)) + (Get-Fibonacci ($n - 2)) } for ($i=0; $i -lt 10; $i++) { Write-Output (Get-Fibonacci $i) }
Simple but inefficient for large n due to repeated calculations.
Using array to store series
powershell
$fib = @(0,1) for ($i=2; $i -lt 10; $i++) { $fib += $fib[$i-1] + $fib[$i-2] } $fib | ForEach-Object { Write-Output $_ }
Stores all numbers, useful if you need to reuse the series later.
Complexity: O(n) time, O(1) space
Time Complexity
The loop runs n times, each iteration does constant work, so time complexity is O(n).
Space Complexity
Only a few variables are used to store numbers, so space complexity is O(1).
Which Approach is Fastest?
The iterative loop approach is fastest and uses least memory compared to recursion or array storage.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative loop | O(n) | O(1) | Efficient printing of series |
| Recursive function | O(2^n) | O(n) | Simple code but slow for large n |
| Array storage | O(n) | O(n) | When series reuse is needed |
Use a loop with two variables to efficiently generate Fibonacci numbers without recursion.
Beginners often forget to update both variables correctly, causing an infinite loop or wrong output.