Last active
September 9, 2019 03:01
-
-
Save talesin/9bdea84ddae902b1af8928e78f0c7de1 to your computer and use it in GitHub Desktop.
fib
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
| let fibn n = | |
| let rec loop i a b = | |
| if i >= n then a | |
| else loop (i+1) b (a+b) | |
| loop 0 0 1 |
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
| fun fibn(n: Int): Int { | |
| tailrec fun loop(i: Int, a: Int, b: Int): Int { | |
| return if (i >= n) a | |
| else loop(i+1, b, a+b) | |
| } | |
| return loop(0, 0, 1) | |
| } |
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
| let fibn = n => { | |
| let rec loop = (i, a, b) => { | |
| if (i >= n) a | |
| else loop(i+1, b, a+b) | |
| } | |
| loop(0, 0, 1) | |
| } |
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
| def fibn(n: Int): Int = { | |
| @scala.annotation.tailrec | |
| def loop(i: Int, a: Int, b: Int): Int = { | |
| if (i >= n) a | |
| else loop(i+1, b, a+b) | |
| } | |
| loop(0, 0, 1) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment