Skip to content

Instantly share code, notes, and snippets.

@InBrewJ
Last active May 23, 2020 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InBrewJ/a7d711d99b64545dd457464386a6933b to your computer and use it in GitHub Desktop.
Save InBrewJ/a7d711d99b64545dd457464386a6933b to your computer and use it in GitHub Desktop.
Iterative Fibonacci Sequence in Scala for no reason whatsoever
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