LeetCode Weekly Contest 25
2017-03-26 22:24
453 查看
507 Perfect Number
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.
Example:
Input: 28
Output: True
Explanation: 28 = 1 + 2 + 4 + 7 + 14
Note: The input number n will not exceed 100,000,000. (1e8)
accepted code
class Solution{ public: bool checkPerfectNumber(int num){ if(num<6) { return false; } int i,j; int sum=1; i=2; while(sum<=num&&i<num) { if(num%i==0) { j=num/i; if(j>i) { sum = sum+i+j; } else break; } i++; } if(num==sum) { return true; } else return false; } };
相关文章推荐
- LeetCode Weekly Contest 25 之 507.Perfect Number
- LeetCode Weekly Contest 25 之 537.Complex Number Multiplication
- LeetCode Weekly Contest 25 之 545.Boundary of Binary Tree
- LeetCode Weekly Contest 40(1) - Average of Levels in Binary Tree
- LeetCode 660 Remove 9 (LeetCode Weekly Contest 45)
- LeetCode Weekly Contest 13-HammingDistance【易】
- LeetCode Weekly Contest 31解题思路
- LeetCode Weekly Contest 44解题思路
- 【LeetCode】LeetCode Weekly Contest 16B
- (Trie)LeetCode Weekly Contest 42 Q4 648. Replace Words
- LeetCode Weekly Contest 13-TotalHamming Distance【中】
- LeetCode Weekly Contest 9 第九周周赛
- leetcode Weekly Contest 67 -- 763. Partition Labels
- LeetCode Weekly Contest 13-Matchsticks to Square【中】
- leetcode weekly contest 61 ( 738. Monotone Increasing Digits )
- LeetCode Weekly Contest 34解题思路
- LeetCode Weekly Contest 28解题思路
- leetcode Weekly Contest 67 -- 765. Couples Holding Hands
- LeetCode Weekly Contest 30解题思路
- LeetCode 650 2 Keys Keyboard - LeetCode Weekly Contest 43