Created
August 12, 2019 08:27
-
-
Save donvito/633ebb037e49200aa023010c07dc3c42 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
x := 10 | |
fmt.Println(sum_i(x)) | |
fmt.Println(sum_r(x)) | |
} | |
//iterate | |
func sum_i(x int) int{ | |
sum := 0 | |
for x > 0{ | |
sum = sum + x | |
x = x - 1 | |
} | |
return sum | |
} | |
//recurse | |
func sum_r(x int) int{ | |
if x < 0{ | |
return 0 | |
} | |
return x + sum_r(x - 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment