{"id":9317,"date":"2021-12-31T12:55:01","date_gmt":"2021-12-31T20:55:01","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9317"},"modified":"2021-12-31T13:01:58","modified_gmt":"2021-12-31T21:01:58","slug":"leetcode-1952-three-divisors","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1952-three-divisors\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1952. Three Divisors"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>n<\/code>, return&nbsp;<code>true<\/code><em>&nbsp;if&nbsp;<\/em><code>n<\/code><em>&nbsp;has&nbsp;<strong>exactly three positive divisors<\/strong>. Otherwise, return&nbsp;<\/em><code>false<\/code>.<\/p>\n\n\n\n<p>An integer&nbsp;<code>m<\/code>&nbsp;is a&nbsp;<strong>divisor<\/strong>&nbsp;of&nbsp;<code>n<\/code>&nbsp;if there exists an integer&nbsp;<code>k<\/code>&nbsp;such that&nbsp;<code>n = k * m<\/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> n = 2\n<strong>Output:<\/strong> false\n<strong>Explantion:<\/strong> 2 has only two divisors: 1 and 2.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4\n<strong>Output:<\/strong> true\n<strong>Explantion:<\/strong> 4 has three divisors: 1, 2, and 4.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Enumerate divisors.<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<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  bool isThree(int n) {\n    int c = 0;\n    for (int i = 1; i <= n; ++i)\n      if (n % i == 0) ++c;        \n    return c == 3;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Optimization<\/strong><\/h2>\n\n\n\n<p>Only need to enumerate divisors up to sqrt(n). Special handle for the d * d == n case.<\/p>\n\n\n\n<p>Time complexity: O(sqrt(n))<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  bool isThree(int n) {\n    int c = 0;\n    for (int i = 1; i <= sqrt(n); ++i)\n      if (n % i == 0)\n        c += 1 + (i * i != n);\n    return c == 3;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;n, return&nbsp;true&nbsp;if&nbsp;n&nbsp;has&nbsp;exactly three positive divisors. Otherwise, return&nbsp;false. An integer&nbsp;m&nbsp;is a&nbsp;divisor&nbsp;of&nbsp;n&nbsp;if there exists an integer&nbsp;k&nbsp;such that&nbsp;n = k * m. Example 1: Input: n&#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,222,31,61],"class_list":["post-9317","post","type-post","status-publish","format-standard","hentry","category-math","tag-divisor","tag-easy","tag-math","tag-sqrt","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9317","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=9317"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9317\/revisions"}],"predecessor-version":[{"id":9322,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9317\/revisions\/9322"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}