Skip to content

Instantly share code, notes, and snippets.

@EmmanuelOga
Created October 20, 2023 01:26
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 EmmanuelOga/0460fcbc411981f2135ea4a7942f468e to your computer and use it in GitHub Desktop.
Save EmmanuelOga/0460fcbc411981f2135ea4a7942f468e to your computer and use it in GitHub Desktop.
Simple recursive algorithm and its cost.
package main
import "fmt"
var cost int
func f2(s, e int) int {
cost += 1
if e <= s {
return 1
}
mid := s + ((e - s) / 2)
return f2(s, mid) + f2(mid+1, e)
}
func main() {
for i := 1; i < 1000; i++ {
cost = 0
f2(0, i)
fmt.Printf("%d\t%d\t\n", i, cost)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment