Skip to content

Instantly share code, notes, and snippets.

@joegasper
Last active February 26, 2017 20:19
Show Gist options
  • Save joegasper/d389f4ccf568a91a707d22b7e37cab33 to your computer and use it in GitHub Desktop.
Save joegasper/d389f4ccf568a91a707d22b7e37cab33 to your computer and use it in GitHub Desktop.
Calculating Fibonacci Sequence to n values or calculating a specific nth value
#Show all values in sequence up to number $seqnum
[int]$seqnum = 10
$fibseq = @()
for($i = 0; $i -lt $seqnum; $i++) {
if ($i -eq 0 ) {
$fibseq = $fibseq + 0
}
if ($i -eq 1 ) {
$fibseq = $fibseq + $i
}
if ($i -gt 1 ) {
$fibseq = $fibseq + ($fibseq[$i-2] + $fibseq[$i-1])
}
}
$fibseq -join ','
#Return only value of the $seqnum number in the sequence
[int]$seqnum = 10
[double]$Phi = 1.618033988749895
$fibseq = 0,1,1,2
if ( $seqnum -le 3) {
if ($seqnum -eq 1 ) {
$fibval = $fibseq[$seqnum-1]
}
if ($seqnum -eq 2 ) {
$fibval = $fibseq[$seqnum-1]
}
if ($seqnum -eq 3 ) {
$fibval = $fibseq[$seqnum-1]
}
} else {
$fibval = [Math]::Round([Math]::Pow($Phi,$seqnum) / ($Phi + 2))
}
$fibval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment