package main

import "fmt"

func sum(start, end uint64, result chan uint64) {
	var sum uint64 = 0
	for i := start; i <= end; i++ {
		sum += i
	}
	result <- sum
}

func main() {
	const n uint64 = 5000000000
	var numCPU int = 4

	result := make(chan uint64, numCPU)

	var chunkSize uint64 = n / uint64(numCPU)

	var start uint64 = 1
	var end uint64 = chunkSize

	for i := 0; i < numCPU; i++ {
		go sum(start, end, result)
		start = end + 1
		end += chunkSize
	}

	var total uint64 = 0
	for i := 0; i < numCPU; i++ {
		partialSum := <-result
		total += partialSum
	}

	fmt.Println(total)
}
