Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added three algorithms for calculating nth Fibbonacci number #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/main/scala/Others/Fibbonacci.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package Others

import scala.annotation.tailrec

/**
* Assuming that sequence starts from 0
*/
object Fibbonacci {
/**
* Standard recursion solution,
*
* @param n - which Fibbonacci number
* @return
*/
def nthNumberRecursive(n: Int): Int = {
if (n <= 1) 1
else nthNumberRecursive(n - 1) + nthNumberRecursive(n - 2)
}

def nthNumberIterative(n: Int): Int = {
var first = 1
var second = 1
if (n == 0) first
else if (n == 1) second
else {
var third: Int = first + second
for (i <- 3 to n) {
first = second
second = third
third = first + second
}
third
}
}

def nthNumberTailRec(n: Int): Int = {
@tailrec
def nthNumber(n: Int, current: Int, temp: Int): Int = n match {
case 0 => current
case _ => nthNumber(n - 1, temp, current + temp)
}

nthNumber(n, 1, 1)
}

}
42 changes: 42 additions & 0 deletions src/test/scala/Others/FibbonacciSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package Others

import org.scalatest.FlatSpec

class FibbonacciSpec extends FlatSpec {

"recursive solution" should "start from 1" in {
assert(Fibbonacci.nthNumberRecursive(0) === 1)
}

it should "calculate some fibbonacci number" in {
assert(Fibbonacci.nthNumberRecursive(4) === 5)
}

it should "return sum of two previous numbers" in {
for (i <- 2 to 10) assert(Fibbonacci.nthNumberRecursive(i) === Fibbonacci.nthNumberRecursive(i - 1) + Fibbonacci.nthNumberRecursive(i - 2))
}

"iterative solution" should "start from 1" in {
assert(Fibbonacci.nthNumberIterative(0) === 1)
}

it should "calculate some fibbonacci number" in {
assert(Fibbonacci.nthNumberIterative(4) === 5)
}

it should "return sum of two previous numbers" in {
for (i <- 2 to 20) assert(Fibbonacci.nthNumberIterative(i) === Fibbonacci.nthNumberIterative(i - 1) + Fibbonacci.nthNumberIterative(i - 2))
}

"tail recursive solution" should "start from 1" in {
assert(Fibbonacci.nthNumberTailRec(0) === 1)
}

it should "calculate some fibbonacci number" in {
assert(Fibbonacci.nthNumberTailRec(4) === 5)
}

it should "return sum of two previous numbers" in {
for (i <- 2 to 20) assert(Fibbonacci.nthNumberTailRec(i) === Fibbonacci.nthNumberTailRec(i - 1) + Fibbonacci.nthNumberTailRec(i - 2))
}
}