Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 757ae04

Browse files
committed
Add problem 507
1 parent eceeb5d commit 757ae04

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

algorithms/507/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 507. Perfect Number
2+
3+
We define the Perfect Number is a **positive** integer that is equal to the sum of all its **positive** divisors except itself.
4+
5+
Now, given an **integer** n, write a function that returns true when it is a perfect number
6+
and false when it is not.
7+
8+
**Example:**
9+
<pre>
10+
<b>Input:</b> 28
11+
<b>Output:<b> True
12+
<b>Explanation:</b> 28 = 1 + 2 + 4 + 7 + 14
13+
</pre>
14+
15+
**Note:** The input number **n** will not exceed 100,000,000. (1e8)

algorithms/507/solution.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import "math"
4+
5+
func checkPerfectNumber(num int) bool {
6+
if num < 2 {
7+
return false
8+
}
9+
sum := 1
10+
for i := 2; i <= int(math.Sqrt(float64(num))); i++ {
11+
if num%i == 0 {
12+
sum += i + num/i
13+
}
14+
}
15+
return sum == num
16+
}

0 commit comments

Comments
 (0)