Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a206224

Browse files
solved: 507. Perfect Number
Signed-off-by: rajput-hemant <rajput.hemant2001@gmail.com>
1 parent b09ba49 commit a206224

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /