{"id":1636,"date":"2018-01-17T22:08:40","date_gmt":"2018-01-18T06:08:40","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1636"},"modified":"2018-01-26T08:18:52","modified_gmt":"2018-01-26T16:18:52","slug":"leetcode-762-prime-number-of-set-bits-in-binary-representation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-762-prime-number-of-set-bits-in-binary-representation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 762. Prime Number of Set Bits in Binary Representation"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 762 Prime Number of Set Bits in Binary Representation  - \u5237\u9898\u627e\u5de5\u4f5c EP160\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/KVKeyehcUuU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u6c42\u7ed9\u5b9a\u8303\u56f4\u5185\uff0c\u6570\u7684\u4e8c\u8fdb\u5236\u5f62\u5f0f\u4e2d1\u7684\u4e2a\u6570\u4e3a\u7d20\u6570\u4e2a\u7684\u6570\u5b57\u7684\u4e2a\u6570\u3002<\/p>\n<p>Given two integers\u00a0<code>L<\/code>\u00a0and\u00a0<code>R<\/code>, find the count of numbers in the range\u00a0<code>[L, R]<\/code>\u00a0(inclusive) having a prime number of set bits in their binary representation.<\/p>\n<p>(Recall that the number of set bits an integer has is the number of\u00a0<code>1<\/code>s present when written in binary. For example,\u00a0<code>21<\/code>\u00a0written in binary is\u00a0<code>10101<\/code>\u00a0which has 3 set bits. Also, 1 is not a prime.)<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\">Input: L = 6, R = 10\r\nOutput: 4\r\nExplanation:\r\n6 -&gt; 110 (2 set bits, 2 is prime)\r\n7 -&gt; 111 (3 set bits, 3 is prime)\r\n9 -&gt; 1001 (2 set bits , 2 is prime)\r\n10-&gt;1010 (2 set bits , 2 is prime)\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true \">Input: L = 10, R = 15\r\nOutput: 5\r\nExplanation:\r\n10 -&gt; 1010 (2 set bits, 2 is prime)\r\n11 -&gt; 1011 (3 set bits, 3 is prime)\r\n12 -&gt; 1100 (2 set bits, 2 is prime)\r\n13 -&gt; 1101 (3 set bits, 3 is prime)\r\n14 -&gt; 1110 (3 set bits, 3 is prime)\r\n15 -&gt; 1111 (4 set bits, 4 is not prime)\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li><code>L, R<\/code>\u00a0will be integers\u00a0<code>L &lt;= R<\/code>\u00a0in the range\u00a0<code>[1, 10^6]<\/code>.<\/li>\n<li><code>R - L<\/code>\u00a0will be at most 10000.<\/li>\n<\/ol>\n<p><strong>Solution 1: Brute Force<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 22 ms\r\nclass Solution {\r\npublic:\r\n  int countPrimeSetBits(int L, int R) {\r\n    int ans = 0;\r\n    for (int n = L; n &lt;= R; ++n)\r\n      if (isPrime(bits(n))) ++ans;\r\n    return ans;\r\n  }\r\nprivate:\r\n  bool isPrime(int n) {\r\n    if (n &lt;= 1) return false;\r\n    if (n == 2) return true;      \r\n    for (int i = 2; i &lt;= sqrt(n); ++i)\r\n      if (n % i == 0) return false;\r\n    return true;\r\n  }\r\n  \r\n  int bits(int n) {\r\n    int s = 0;\r\n    while (n) {\r\n      s += n &amp; 1;\r\n      n &gt;&gt;= 1;\r\n    }        \r\n    return s;\r\n  }\r\n};<\/pre>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  int countPrimeSetBits(int L, int R) {\r\n    int ans = 0;\r\n    set&lt;int&gt; primes{2, 3, 5, 7, 11, 13, 17, 19};\r\n    for (int n = L; n &lt;= R; ++n)\r\n      if (primes.count(__builtin_popcountll(n))) ++ans;\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 30 ms\r\nclass Solution {\r\npublic:\r\n  int countPrimeSetBits(int L, int R) {\r\n    int ans = 0;\r\n    unordered_set&lt;int&gt; primes{2, 3, 5, 7, 11, 13, 17, 19};\r\n    for (int n = L; n &lt;= R; ++n)\r\n      if (primes.count(__builtin_popcountll(n))) ++ans;\r\n    return ans;\r\n  }\r\n};<\/pre>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 9 ms\r\nclass Solution {\r\npublic:\r\n  int countPrimeSetBits(int L, int R) {\r\n    int ans = 0;\r\n    vector&lt;int&gt; p(20, 0);\r\n    p[2] = p[3] = p[5] = p[7] = p[11] = p[13] = p[17] = p[19] = 1;\r\n    for (int n = L; n &lt;= R; ++n)\r\n      if (p[__builtin_popcountll(n)]) ++ans;\r\n    return ans;\r\n  }\r\n};<\/pre>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 8 ms\r\nclass Solution {\r\npublic:\r\n  int countPrimeSetBits(int L, int R) {\r\n    constexpr int magic = 665772;\r\n    int ans = 0;\r\n    for (int n = L; n &lt;= R; ++n)\r\n      if (magic &amp; (1 &lt;&lt; __builtin_popcountll(n))) ++ans;\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 39 ms\r\nclass Solution {\r\n  public int countPrimeSetBits(int L, int R) {\r\n    int ans = 0;\r\n    for (int n = L; n &lt;= R; ++n)\r\n      if (isPrime(bits(n))) ++ans;\r\n    return ans;\r\n  }\r\n  \r\n  private boolean isPrime(int n) {\r\n    if (n &lt;= 1) return false;\r\n    if (n == 2) return true;      \r\n    for (int i = 2; i &lt;= (int)Math.sqrt(n); ++i)\r\n      if (n % i == 0) return false;\r\n    return true;\r\n  }\r\n  \r\n  private int bits(int n) {\r\n    int s = 0;\r\n    while (n != 0) {\r\n      s += n &amp; 1;\r\n      n &gt;&gt;= 1;\r\n    }        \r\n    return s;\r\n  }\r\n}<\/pre>\n<p>Python2<\/p>\n<pre class=\"lang:default decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 1618 ms\r\n\"\"\"\r\nclass Solution(object):\r\n  def countPrimeSetBits(self, L, R):\r\n    def isPrime(n):\r\n      if n &lt;= 1: return False\r\n      if n == 2: return True\r\n      for i in range(2, int(math.sqrt(n)) + 1):\r\n        if n % i == 0: return False\r\n      return True\r\n\r\n    def bits(n):\r\n      s = 0\r\n      while n != 0:\r\n        s += n &amp; 1\r\n        n &gt;&gt;= 1\r\n      return s\r\n\r\n    ans = 0\r\n    for n in range(L, R + 1):\r\n      if isPrime(bits(n)): ans += 1\r\n    return ans<\/pre>\n<p>Python2<\/p>\n<pre class=\"lang:python decode:true\">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 1163 ms\r\n\"\"\"\r\nclass Solution(object):\r\n  def countPrimeSetBits(self, L, R):\r\n    def bits(n):\r\n      s = 0\r\n      while n != 0:\r\n        s += n &amp; 1\r\n        n &gt;&gt;= 1\r\n      return s\r\n    \r\n    primes = set([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31])\r\n\r\n    ans = 0\r\n    for n in range(L, R + 1):\r\n      if bits(n) in primes: ans += 1\r\n    return ans<\/pre>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 667 ms\r\n\"\"\"\r\nclass Solution(object):   \r\n  def countPrimeSetBits(self, L, R):\r\n    def bits(n):\r\n      s = 0\r\n      while n != 0:\r\n        n &amp;= n - 1;\r\n        s += 1\r\n      return s\r\n        \r\n    ans = 0\r\n    while L &lt;= R:\r\n      if (665772 &gt;&gt; bits(L)) &amp; 1 &gt; 0: ans += 1\r\n      L += 1\r\n\r\n    return ans<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u6c42\u7ed9\u5b9a\u8303\u56f4\u5185\uff0c\u6570\u7684\u4e8c\u8fdb\u5236\u5f62\u5f0f\u4e2d1\u7684\u4e2a\u6570\u4e3a\u7d20\u6570\u4e2a\u7684\u6570\u5b57\u7684\u4e2a\u6570\u3002 Given two integers\u00a0L\u00a0and\u00a0R, find the count of numbers in the range\u00a0[L, R]\u00a0(inclusive) having a prime number of set bits in their binary representation. (Recall&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126,46,163],"tags":[16,59],"class_list":["post-1636","post","type-post","status-publish","format-standard","hentry","category-bit","category-dynamic-programming","category-easy","tag-bit","tag-prime","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1636","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=1636"}],"version-history":[{"count":13,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1636\/revisions"}],"predecessor-version":[{"id":1670,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1636\/revisions\/1670"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}