Skip to content

Instantly share code, notes, and snippets.

@fujiwara
Created November 11, 2009 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujiwara/231881 to your computer and use it in GitHub Desktop.
Save fujiwara/231881 to your computer and use it in GitHub Desktop.
package main
import (
"fmt";
"os";
"strconv";
)
func main() {
n, error := strconv.Atoi( os.Args[1] );
if ( error != nil ) {
os.Exit(1);
}
ch := make(chan int, 2);
gofib(ch, n, 0);
fmt.Printf("fib(%d) = %d\n", n, <-ch);
}
func gofib( ch chan int, n int, level int ) int {
var result int;
if ( n == 0 ) {
result = 0;
} else if ( n == 1 ) {
result = 1;
} else {
sub_ch := make(chan int, 2);
go gofib(sub_ch, n - 2, level + 1);
go gofib(sub_ch, n - 1, level + 1);
a := <-sub_ch;
b := <-sub_ch;
result = a + b;
}
ch <- result;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment