-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
32 lines (28 loc) · 866 Bytes
/
Solution.cs
File metadata and controls
32 lines (28 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
namespace LeetCode.Problem507{
//507. Perfect Number
//https://leetcode.com/problems/perfect-number/
/*
A perfect number is a positive integer that is equal to the sum of its positive divisors,
excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.
Given an integer n, return true if n is a perfect number, otherwise return false.
*/
public class Solution {
public bool CheckPerfectNumber(int num) {
var divs = 0;
var index = 1;
while (index < num)
{
if (num % index == 0)
divs += index;
index++;
}
return divs == num ? true : false;
}
}
public class Alternative {
public bool CheckPerfectNumber(int num) {
var set = new HashSet<int>(){ 6, 28, 496, 8128, 33550336 };
return set.Contains(num);
}
}
}