-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path507.cpp
43 lines (40 loc) · 1.09 KB
/
507.cpp
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
33
34
35
36
37
38
39
40
41
42
43
__________________________________________________________________________________________________
sample 4 ms submission
class Solution {
public:
bool checkPerfectNumber(int num) {
if(num==1)
return false;
int sum = 1 ;
for(int i=2;i*i<num;i++)
{
if(num%i==0)
{
sum = sum + i + (num/i);
}
}
if(sum==num)
return true;
return false;
}
};
__________________________________________________________________________________________________
sample 8368 kb submission
class Solution {
public:
bool checkPerfectNumber(int num) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if(num==1) return false;
int sq = sqrt(num);
int ans = 1;
for(int i=2 ; i<=sq ; i++){
if(num%i==0){
ans+=i+num/i;
}
}
return ans==num?true:false;
}
};
__________________________________________________________________________________________________