Skip to content

Commit 38f6ec6

Browse files
committed
p728
1 parent 29fea90 commit 38f6ec6

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

algorithms/p728/728.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package p728
2+
3+
func isSelfDividingNumber(num int) bool {
4+
v := num
5+
for v != 0 {
6+
d := v % 10
7+
v = v / 10
8+
if d == 0 || num%d != 0 {
9+
return false
10+
}
11+
}
12+
return true
13+
}
14+
15+
func selfDividingNumbers(left int, right int) []int {
16+
res := make([]int, 0)
17+
for i := left; i <= right; i++ {
18+
if isSelfDividingNumber(i) {
19+
res = append(res, i)
20+
}
21+
}
22+
return res
23+
}

algorithms/p728/728_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package p728
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test0(t *testing.T) {
10+
11+
res := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22}
12+
assert.Equal(t, res, selfDividingNumbers(1, 22))
13+
}

0 commit comments

Comments
 (0)