// Week 12 – Recursion depth in Go: the stack grows dynamically (at most 1 GB by default)
// Run: go run ./w12/stackdepth
package main

import "fmt"

func sumTo(n int64) int64 {
	if n == 0 {
		return 0
	}
	return n + sumTo(n-1)
}

func main() {
	fmt.Println("sumTo(5000) =", sumTo(5000))
	// in Java this would be a StackOverflowError; in Go the stack of the goroutine grows automatically
	fmt.Println("sumTo(1 000 000) =", sumTo(1_000_000))
	// Infinite recursion crashes in Go too ("goroutine stack exceeds 1000000000-byte limit"),
	// and this cannot be caught – so we do not run it here.
	fmt.Println("Iteratively it is always safe: n·(n+1)/2 =", int64(1_000_000)*1_000_001/2)
}
