diff --git a/cmd/21/main.go b/cmd/21/main.go new file mode 100644 index 0000000..f144b34 --- /dev/null +++ b/cmd/21/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "math" +) + +const maxLen = 10000 + +func main() { + divList := make([]int, maxLen) + numList := make([]int, 0, 100) + for i := 0; i < maxLen; i+=1 { + if divList[i] > 0 { + continue + } + sum := sumDiv(findDiv(i)) + divList[i] = sum + + if sum >= maxLen { + continue + } + + if divList[sum] == 0 { + divList[sum] = sumDiv(findDiv(sum)) + } + + if divList[sum] == i && sum != i { + numList = append(numList, i, sum) + } + } + + fmt.Printf("List: %v\n", numList) + fmt.Printf("Sum: %d\n", sumDiv(numList)) +} + +func findDiv(num int) []int { + out := []int{1} + minDiv2 := num + for i := 2; i < int(math.Sqrt(float64(num))); i+=1 { + if i == minDiv2 { + break + } + if num % i == 0 { + div2 := num / i + out = append(out, i, div2) + minDiv2 = div2 + } + } + + return out +} + +func sumDiv(list []int) int { + sum := 0 + for _, i := range list { + sum += i + } + + return sum +}