Skip to content

Commit e21eea8

Browse files
committed
add solution problem 507
1 parent b57acc9 commit e21eea8

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/0507.Perfect-Number/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# [507. Perfect Number][title]
2+
3+
## Description
4+
5+
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.
6+
7+
Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.
8+
9+
**Example 1:**
10+
11+
```
12+
Input: 28
13+
Output: True
14+
Explanation: 28 = 1 + 2 + 4 + 7 + 14
15+
```
16+
17+
18+
19+
[title]: https://leetcode.com/problems/perfect-number/

src/0507.Perfect-Number/Solution.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package Solution
2+
3+
func checkPerfectNumber(num int) bool {
4+
if num == 0 {
5+
return false
6+
}
7+
sum := 0
8+
9+
for i := 1; i <= num/2; i++ {
10+
if num%i == 0 {
11+
sum += i
12+
13+
}
14+
15+
}
16+
return sum == num
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestCheckPerfectNumber(t *testing.T) {
9+
cases := []struct {
10+
name string
11+
input int
12+
expected bool
13+
}{
14+
{"Test case 1", 28, true},
15+
}
16+
17+
for _, testcase := range cases {
18+
t.Run(testcase.name, func(t *testing.T) {
19+
got := checkPerfectNumber(testcase.input)
20+
if !reflect.DeepEqual(got, testcase.expected) {
21+
t.Fatalf("expected: %v, but got: %v, with input: %v",
22+
testcase.expected, got, testcase.input)
23+
}
24+
})
25+
}
26+
}

0 commit comments

Comments
 (0)