<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sieve Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/sieve/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/sieve/</link>
	<description></description>
	<lastBuildDate>Thu, 05 Jul 2018 23:16:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>sieve Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/sieve/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 204. Count Primes</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-204-count-primes/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-204-count-primes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 09 Sep 2017 08:39:47 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[prime]]></category>
		<category><![CDATA[sieve]]></category>
		<category><![CDATA[sqrt]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=168</guid>

					<description><![CDATA[<p>Problem: Count the number of prime numbers less than a non-negative number, n. Idea: Sieve of Eratosthenes Time Complexity: O(nloglogn) Space Complexity: O(n) Solution: C++ [crayon-663c8f93bbe27864883134/]&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-204-count-primes/">花花酱 LeetCode 204. Count Primes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/Kwo2jkHOyPY?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Count the number of prime numbers less than a non-negative number, <b><i>n</i></b>.</p>
<p><strong>Idea:</strong></p>
<p><a href="https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a></p>
<p><strong>Time Complexity:</strong></p>
<p>O(nloglogn)</p>
<p><strong>Space Complexity:</strong></p>
<p>O(n)</p>
<p><strong>Solution:</strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    int countPrimes(int n) {
        if (n &lt; 3) return 0;
        
        vector&lt;unsigned char&gt; f(n, 1);
        f[0] = f[1] = 0;
        for(long i=2;i&lt;=sqrt(n);++i) {
            if(!f[i]) continue;
            for(long j=i*i;j&lt;n;j+=i)
                f[j] = 0;
        }
        
        int ans = accumulate(f.begin(), f.end(), 0);
        return ans;
    }
};</pre><p>Java</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 16 ms
class Solution {
  public int countPrimes(int n) {
    int ans = 0;
    boolean[] isPrime = new boolean[n + 1];
    Arrays.fill(isPrime, true);
    isPrime[0] = false;
    if (n &gt; 0) isPrime[1] = false;
    for (int i = 2; i &lt; n; ++i) {
      if (!isPrime[i]) continue;
      ++ans;
      for (int j = 2 * i; j &lt; n; j += i)
        isPrime[j] = false;
    }
    return ans;
  }
}</pre><p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 800 ms
"""
class Solution:
  def countPrimes(self, n):
    if n &lt; 3: return 0
    isPrime = [True] * (n + 1)
    isPrime[0] = False
    isPrime[1] = False
    ans = 0
    for i in range(2, n):
      if not isPrime[i]: continue
      ans += 1
      for j in range(2 * i, n, i):
        isPrime[j] = False
    return ans</pre><p>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-204-count-primes/">花花酱 LeetCode 204. Count Primes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-204-count-primes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
