summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCasey Robinson <casey@rampantmonkey.com>2015-06-15 12:54:31 -0400
committerCasey Robinson <casey@rampantmonkey.com>2015-06-15 12:54:31 -0400
commit0d4d6ac4a35d625df15f4bcd51a445d83e6c5f5f (patch)
tree71c03c7a2bc2afe358ae291e3751108fd2aea36d
parente0373b3bec157ee05c84f5970854d74c7f330cf0 (diff)
downloadfibgo-0d4d6ac4a35d625df15f4bcd51a445d83e6c5f5f.tar.gz
fibgo-0d4d6ac4a35d625df15f4bcd51a445d83e6c5f5f.tar.bz2
fibgo-0d4d6ac4a35d625df15f4bcd51a445d83e6c5f5f.zip
trivial recursive solution
-rw-r--r--main.go15
1 files changed, 15 insertions, 0 deletions
diff --git a/main.go b/main.go
index 836545d..a1a5ffc 100644
--- a/main.go
+++ b/main.go
@@ -24,6 +24,10 @@ func main() {
fallthrough
case "iterative":
fmt.Printf("fib(%d) = %d\n", n, iterative(n))
+ case "r":
+ fallthrough
+ case "recursive":
+ fmt.Printf("fib(%d) = %d\n", n, recursive(n))
default:
fmt.Printf("Unknown/unimplemented method %s\n", *method)
}
@@ -38,3 +42,14 @@ func iterative(n int) int {
}
return a
}
+
+func recursive(n int) int {
+ switch n {
+ case 0:
+ return 0
+ case 1:
+ return 1
+ default:
+ return recursive(n-1) + recursive(n-2)
+ }
+}