Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:21
Show Gist options
  • Select an option

  • Save dacr/27218e2232fb4a76753bf52996191798 to your computer and use it in GitHub Desktop.

Select an option

Save dacr/27218e2232fb4a76753bf52996191798 to your computer and use it in GitHub Desktop.
compute fibonacci number / published by https://github.com/dacr/code-examples-manager #0e363d0a-3312-480c-8ceb-69c547dce4a6/984f9b16dcf2e54b0b5a305896703a678feabc04
// summary : compute fibonacci number
// keywords : scala, math, fibonacci, @testable
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : 0e363d0a-3312-480c-8ceb-69c547dce4a6
// created-on : 2021-04-06T10:48:09Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.4.2"
//> using dep "org.scalatest::scalatest:3.2.16"
//> using objectWrapper
// ---------------------
import org.scalatest._
import flatspec._
import matchers._
// TODO - to be improved
def fib(n:BigInt):BigInt = {
if (n<=0) 0
else if (n ==1) 1
else fib(n-1)+fib(n-2)
}
class FactTest extends AnyFlatSpec with should.Matchers {
override def suiteName: String = "FactTest"
"fibonacci" should "return the right result" in {
fib(0) shouldBe 0
fib(1) shouldBe 1
fib(2) shouldBe 1
fib(3) shouldBe 2
fib(4) shouldBe 3
fib(16) shouldBe 987
fib(20) shouldBe 6765
fib(30) shouldBe 832040
fib(40) shouldBe 102334155
//fib(49) shouldBe BigInt("7778742049")
//fib(50) shouldBe BigInt("12586269025")
//fib(100) shouldBe BigInt("354224848179261915075")
//fib(200) shouldBe BigInt("280571172992510140037611932413038677189525")
//fib(300) shouldBe BigInt("222232244629420445529739893461909967206666939096499764990979600")
}
}
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[FactTest].getName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment