Avatar
@swift-5.4.3 let n = 5 var sumToCount: [Int: Int] = [:] for i in 1 ... n { for j in 1 ... n { for k in 1 ... n { let sum = i + j + k sumToCount[sum, default: 0] += 1 } } } for sum in sumToCount.keys.sorted() { print(sum, sumToCount[sum]!) }