Last active
May 25, 2024 10:20
-
-
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/1b3f99148294266dd9687061e197439c96e584a2
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
// 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.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