Skip to content

Commit a206224

Browse files
committed
solved: 507. Perfect Number
Signed-off-by: rajput-hemant <[email protected]>
1 parent b09ba49 commit a206224

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn check_perfect_number(num: i32) -> bool {
3+
if num == 1 {
4+
return false;
5+
}
6+
7+
let (mut sum, mut i) = (1, 2);
8+
9+
while i * i <= num {
10+
// if i is a factor of num, add i and num / i to sum
11+
if num % i == 0 {
12+
sum += i;
13+
if i * i != num {
14+
sum += num / i;
15+
}
16+
}
17+
i += 1;
18+
}
19+
20+
sum == num
21+
}
22+
}

0 commit comments

Comments
 (0)