Fibonacci sequence: Difference between revisions
Content deleted Content added
BirdCatNate (talk | contribs) Added a solution and explanation for Rhovas |
BirdCatNate (talk | contribs) m Tweaked implementation for readability |
||
Line 10,946: | Line 10,946: | ||
This solution works for negative numbers |
This solution works for negative numbers |
||
<syntaxhighlight lang="scala"> |
<syntaxhighlight lang="scala"> |
||
// Calculate nth positive fibonacci number |
|||
⚫ | |||
func pFib(n: Integer): Integer { |
|||
⚫ | |||
⚫ | |||
var lastFib = -1; |
var lastFib = -1; |
||
var fib = 1; |
var fib = 1; |
||
Line 10,958: | Line 10,956: | ||
}; |
}; |
||
return fib; |
return fib; |
||
} |
|||
// Calculate nth integer fibonacci number |
|||
⚫ | |||
match ([n < 0, n.mod(2)]){ |
|||
[true, 0]: return -pFib(-n); |
|||
⚫ | |||
⚫ | |||
} |
|||
} |
} |
||
</syntaxhighlight> |
</syntaxhighlight> |