Last active
May 23, 2020 10:02
-
-
Save InBrewJ/a7d711d99b64545dd457464386a6933b to your computer and use it in GitHub Desktop.
Iterative Fibonacci Sequence in Scala for no reason whatsoever
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object fibonnaci { | |
var lastMinusOne = 0 | |
var last = 0 | |
for (count <- 1 to 10) { | |
if (count == 2) lastMinusOne = lastMinusOne + 1 | |
println({val current = last + lastMinusOne; current }) | |
val tempLast = last | |
last = last + lastMinusOne // aka current | |
lastMinusOne = tempLast | |
} | |
// Alternatively | |
var lastMinusOne_f: Float = 0.0f //> lastMinusOne : Float = 0.0 | |
var last_f: Float = 0.0f //> last : Float = 0.0 | |
for (count <- 1 to 40) { | |
val current = last_f + lastMinusOne_f | |
lastMinusOne_f = last_f | |
last_f = current | |
if (count == 1) lastMinusOne_f = 1 | |
if (count == 2) last_f = 1 | |
println(current) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment