{"id":2308,"date":"2018-03-22T22:23:54","date_gmt":"2018-03-23T05:23:54","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2308"},"modified":"2018-03-22T22:29:50","modified_gmt":"2018-03-23T05:29:50","slug":"leetcode-507-perfect-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-507-perfect-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 507. Perfect Number"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u5224\u65ad\u4e00\u4e2a\u6570\u7684\u56e0\u6570\u548c\u662f\u4e0d\u662f\u7b49\u4e8e\u5b83\u672c\u8eab\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/perfect-number\/description\/\">https:\/\/leetcode.com\/problems\/perfect-number\/description\/<\/a><\/p>\n<p>We define the Perfect Number is a\u00a0<b>positive<\/b>\u00a0integer that is equal to the sum of all its\u00a0<b>positive<\/b>\u00a0divisors except itself.<\/p>\n<p>Now, given an\u00a0<b>integer<\/b>\u00a0n, write a function that returns true when it is a perfect number and false when it is not.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b> 28\r\n<b>Output:<\/b> True\r\n<b>Explanation:<\/b> 28 = 1 + 2 + 4 + 7 + 14\r\n<\/pre>\n<p><b>Note:<\/b>\u00a0The input number\u00a0<b>n<\/b>\u00a0will not exceed 100,000,000. (1e8)<\/p>\n<h1><strong>Solution: Brute Force<\/strong><\/h1>\n<p>Try allnumbers from 1 to n &#8211; 1.<\/p>\n<p>Time complexity: O(n) TLE for prime numbers<\/p>\n<p>Space complexity: O(1)<\/p>\n<h1><strong>Solution: Math<\/strong><\/h1>\n<p>Try all numbers from 2 to sqrt(n).<\/p>\n<p>If\u00a0number i is a divisor of n then n\/i is another one.<\/p>\n<p>Be careful about the case when i == n\/i, only one should be added to the sum.<\/p>\n<p>Time complexity: O(sqrt(n))<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 6 ms\r\nclass Solution {\r\npublic:\r\n  bool checkPerfectNumber(int num) {\r\n    if (num &lt;= 1) return false;    \r\n    int sum = 1;\r\n    for (int i = 2; i &lt;= sqrt(num); ++i)\r\n      if (num % i == 0) sum += i + ((i == num \/ i) ? 0 : num \/ i);   \r\n    return sum == num;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u5224\u65ad\u4e00\u4e2a\u6570\u7684\u56e0\u6570\u548c\u662f\u4e0d\u662f\u7b49\u4e8e\u5b83\u672c\u8eab\u3002 https:\/\/leetcode.com\/problems\/perfect-number\/description\/ We define the Perfect Number is a\u00a0positive\u00a0integer that is equal to the sum of all its\u00a0positive\u00a0divisors except itself. Now, given an\u00a0integer\u00a0n, write&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[222,31,61],"class_list":["post-2308","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-math","tag-sqrt","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2308"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2308\/revisions"}],"predecessor-version":[{"id":2313,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2308\/revisions\/2313"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}