{"id":6539,"date":"2020-03-22T14:06:57","date_gmt":"2020-03-22T21:06:57","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6539"},"modified":"2020-03-22T14:10:05","modified_gmt":"2020-03-22T21:10:05","slug":"leetcode-1390-four-divisors","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1390-four-divisors\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1390. Four Divisors"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>, return the sum of divisors of the integers in that array that have exactly four divisors.<\/p>\n\n\n\n<p>If there is no such integer in the array, return&nbsp;<code>0<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [21,4,7]\n<strong>Output:<\/strong> 32\n<strong>Explanation:<\/strong>\n21 has 4 divisors: 1, 3, 7, 21\n4 has 3 divisors: 1, 2, 4\n7 has 2 divisors: 1, 7\nThe answer is the sum of divisors of 21 only.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10^4<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>If a number is a perfect square (e.g. 9 = 3 * 3), it will have odd number of divisors. (9: 1, 3, 9).<\/p>\n\n\n\n<p>Time complexity: O(sum(sqrt(num_i))<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int sumFourDivisors(vector<int>& nums) {\n    int ans = 0;\n    for (int n : nums) {\n      int r = sqrt(n);\n      if (n <= 4 || r * r == n) continue;\n      int count = 2;\n      int sum = 1 + n;\n      for (int d = 2; d <= r; ++d)\n        if (n % d == 0) {\n          count += 2;\n          sum += n \/ d + d;\n        }      \n      if (count == 4) ans += sum;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such&#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":[574,31,61],"class_list":["post-6539","post","type-post","status-publish","format-standard","hentry","category-math","tag-divisor","tag-math","tag-sqrt","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6539","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=6539"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6539\/revisions"}],"predecessor-version":[{"id":6541,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6539\/revisions\/6541"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}