You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
lavos/cap3/ex2.go

26 lines
548 B

package cap3
// Ex2 例2 求1000以内的完数,返回一个map,key为完数,value为其因数的数组
func Ex2() map[int][]int {
result := map[int][]int{}
for i := range 1000 {
if i < 2 {
continue
}
sum := 0
factors := make([]int, 0, 20)
for j := 1; j <= i/2; j++ {
if i%j == 0 {
factors = append(factors, j)
sum += j
}
}
if sum == i {
result[i] = factors
}
}
return result
}