summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCasey Robinson <casey@rampantmonkey.com>2015-06-15 12:39:22 -0400
committerCasey Robinson <casey@rampantmonkey.com>2015-06-15 12:39:22 -0400
commite0373b3bec157ee05c84f5970854d74c7f330cf0 (patch)
tree4b739507c66a152e169eb342939ebb0877ea3dca
parent3dab3612aa95e5a7411ba1f2c1518507b58a1f66 (diff)
downloadfibgo-e0373b3bec157ee05c84f5970854d74c7f330cf0.tar.gz
fibgo-e0373b3bec157ee05c84f5970854d74c7f330cf0.tar.bz2
fibgo-e0373b3bec157ee05c84f5970854d74c7f330cf0.zip
simple iterative solution
-rw-r--r--.gitignore1
-rw-r--r--main.go40
2 files changed, 41 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1f0438d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+fibgo
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..836545d
--- /dev/null
+++ b/main.go
@@ -0,0 +1,40 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "os"
+ "strconv"
+)
+
+func main() {
+ method := flag.String("m", "iterative", "choose the method for computing nth fibonacci number")
+ flag.Parse()
+ if len(flag.Args()) < 1 {
+ fmt.Printf("n not specified\n")
+ os.Exit(1)
+ }
+ n, err := strconv.Atoi(flag.Args()[0])
+ if err != nil {
+ fmt.Printf("invalid value for n: %v\n", err)
+ }
+ flag.Args()
+ switch *method {
+ case "i":
+ fallthrough
+ case "iterative":
+ fmt.Printf("fib(%d) = %d\n", n, iterative(n))
+ default:
+ fmt.Printf("Unknown/unimplemented method %s\n", *method)
+ }
+}
+
+func iterative(n int) int {
+ a := 0
+ b := 1
+ for i := 0; i < n; i++ {
+ a += b
+ a, b = b, a
+ }
+ return a
+}