How to resolve the algorithm Fibonacci sequence step by step in the PowerShell programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Fibonacci sequence step by step in the PowerShell programming language
Table of Contents
Problem Statement
The Fibonacci sequence is a sequence Fn of natural numbers defined recursively:
Write a function to generate the nth Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow and are mostly used as an exercise in recursion). The sequence is sometimes extended into negative numbers by using a straightforward inverse of the positive definition: support for negative n in the solution is optional.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fibonacci sequence step by step in the PowerShell programming language
Source code in the powershell programming language
function FibonacciNumber ( $count )
{
$answer = @(0,1)
while ($answer.Length -le $count)
{
$answer += $answer[-1] + $answer[-2]
}
return $answer
}
$count = 8
$answer = @(0,1)
0..($count - $answer.Length) | Foreach { $answer += $answer[-1] + $answer[-2] }
$answer
function fib($n) {
switch ($n) {
0 { return 0 }
1 { return 1 }
{ $_ -lt 0 } { return [Math]::Pow(-1, -$n + 1) * (fib (-$n)) }
default { return (fib ($n - 1)) + (fib ($n - 2)) }
}
}
You may also check:How to resolve the algorithm Gray code step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Universal Turing machine step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the Raku programming language
You may also check:How to resolve the algorithm Write entire file step by step in the Haskell programming language
You may also check:How to resolve the algorithm Classes step by step in the zkl programming language