Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 27, 2023 06:29
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 dacr/27218e2232fb4a76753bf52996191798 to your computer and use it in GitHub Desktop.
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/345192e57d98e726be94da26338fd4e716afd97a
// summary : compute fibonacci number
// keywords : scala, math, fibonacci, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// 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.3.0"
//> 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