mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 18:44:44 +01:00
23: non-abundant sums
This commit is contained in:
@@ -5,7 +5,7 @@ import "fmt"
|
||||
func main() {
|
||||
n := 20
|
||||
for {
|
||||
if IsDivisibleFor1to20(n) {
|
||||
if IsDivisible(1, 20, n) {
|
||||
fmt.Println("evenly divisible by all of the numbers from 1 to 20:", n)
|
||||
return
|
||||
}
|
||||
@@ -13,8 +13,8 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func IsDivisibleFor1to20(n int) bool {
|
||||
for i := 1; i <= 20; i++ {
|
||||
func IsDivisible(start, end, n int) bool {
|
||||
for i := start; i <= end; i++ {
|
||||
if n%i != 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -2,19 +2,39 @@ package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIsDivisibleFor1to20(t *testing.T) {
|
||||
func Test_isDivisible(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
start int
|
||||
end int
|
||||
input int
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
"2",
|
||||
1,
|
||||
2,
|
||||
2,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"10",
|
||||
1,
|
||||
10,
|
||||
2520,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"20",
|
||||
1,
|
||||
20,
|
||||
20,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"14",
|
||||
1,
|
||||
20,
|
||||
14,
|
||||
false,
|
||||
},
|
||||
@@ -22,9 +42,9 @@ func TestIsDivisibleFor1to20(t *testing.T) {
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := IsDivisibleFor1to20(tc.input)
|
||||
got := IsDivisible(tc.start, tc.end, tc.input)
|
||||
if got != tc.want {
|
||||
t.Errorf("IsDivisibleFor1to20(%d)=%v, want %v", tc.input, got, tc.want)
|
||||
t.Errorf("IsDivisible(%d)=%v, want %v", tc.input, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
82
23_non_abundant_sums/abundant_test.go
Normal file
82
23_non_abundant_sums/abundant_test.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_isAbundantNumber(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
input int
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
"1",
|
||||
1,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"12",
|
||||
12,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"28",
|
||||
28,
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := isAbundantNumber(tc.input)
|
||||
if got != tc.want {
|
||||
t.Errorf("isAbundantNumber() = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_sumOfDivisors(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
input int
|
||||
want int
|
||||
}{
|
||||
{
|
||||
"1",
|
||||
1,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"2",
|
||||
2,
|
||||
1,
|
||||
},
|
||||
{
|
||||
"4",
|
||||
4,
|
||||
3,
|
||||
},
|
||||
{
|
||||
"12",
|
||||
12,
|
||||
16,
|
||||
},
|
||||
{
|
||||
"28",
|
||||
28,
|
||||
28,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := sumOfDivisors(tc.input)
|
||||
if !reflect.DeepEqual(got, tc.want) {
|
||||
t.Errorf("sumOfDivisors() = %d, want %d", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
55
23_non_abundant_sums/main.go
Normal file
55
23_non_abundant_sums/main.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
sum := 0
|
||||
var nums []int
|
||||
ans := make(map[int]struct{})
|
||||
for i := 1; i < 28123; i++ {
|
||||
if isAbundantNumber(i) {
|
||||
ans[i] = struct{}{}
|
||||
}
|
||||
find := false
|
||||
for k := range ans {
|
||||
d := i - k
|
||||
if d < 0 {
|
||||
break
|
||||
}
|
||||
if _, f := ans[d]; f {
|
||||
fmt.Printf("%d = %d + %d\n", i, k, d)
|
||||
find = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !find {
|
||||
sum += i
|
||||
nums = append(nums, i)
|
||||
}
|
||||
}
|
||||
fmt.Println("abundant numbers:", ans)
|
||||
fmt.Println("sum:", sum, "nums:", nums)
|
||||
}
|
||||
|
||||
func isAbundantNumber(input int) bool {
|
||||
return sumOfDivisors(input) > input
|
||||
}
|
||||
|
||||
func sumOfDivisors(input int) int {
|
||||
if input < 2 {
|
||||
return 0
|
||||
}
|
||||
sum := 0
|
||||
i := 1
|
||||
for i*i <= input {
|
||||
if input%i == 0 {
|
||||
sum += i
|
||||
j := input / i
|
||||
if j != i && j != input {
|
||||
sum += j
|
||||
}
|
||||
}
|
||||
i++
|
||||
}
|
||||
return sum
|
||||
}
|
||||
Reference in New Issue
Block a user