<?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>Re: Nim埃氏筛法 - Code666 (代码贴、代码片段)</title>
    <description>Re: Nim埃氏筛法 - Code666 (代码贴、代码片段)</description>
    <atom:link href="http://www.code666.cn/view/rss/744a4ef5" rel="self" type="application/rss+xml" />
    <link>http://www.code666.cn/</link>
    <language>en</language>
<item>
    <title>Re: Re: Nim埃氏筛法</title>
    <link>http://www.code666.cn/view/375a1bda</link>
    <pubDate>Sun, 15 Jan 2023 20:54:23 +0800</pubDate>
    <dc:creator>4n0n4me</dc:creator>
    <guid isPermaLink="false">http://www.code666.cn/view/375a1bda</guid>
    <description><![CDATA[# 又错了啦，我是傻逼 import strutils, strformat var n = parseInt(readLine(stdin)) # 要求质数的范围上限 numbers: seq[Natural] = @[] # 剩余未判断素性的数字 primes: seq[Natural] = @[] # 质数们 for i in 2..n: numbers.add(i) # 初始化 while&#8230;]]></description>
    <content:encoded><![CDATA[<div class="nimrod" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #808080; font-style: italic;"># 又错了啦，我是傻逼</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;font-weight:bold;">import</span> strutils, strformat</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;font-weight:bold;">var</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; n <span style="color: #66cc66;">=</span> parseInt<span style="color: black;">&#40;</span><span style="color: #dc143c;">readLine</span><span style="color: black;">&#40;</span>stdin<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># 要求质数的范围上限</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; numbers: <span style="color: #0000cd;">seq</span><span style="color: black;">&#91;</span>Natural<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">@</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> &nbsp; <span style="color: #808080; font-style: italic;"># 剩余未判断素性的数字</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; primes: <span style="color: #0000cd;">seq</span><span style="color: black;">&#91;</span>Natural<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">@</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> &nbsp; &nbsp;<span style="color: #808080; font-style: italic;"># 质数们</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span class="nu0">2</span><span style="color: #66cc66;">..</span><span class="nu0">n</span>:</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; numbers.<span style="color: #dc143c;">add</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># 初始化</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #dc143c;">len</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span class="nu0">0</span>:</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">var</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; p <span style="color: #66cc66;">=</span> numbers<span style="color: black;">&#91;</span><span class="nu0">0</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># 最小的质数</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; newNumbers: <span style="color: #0000cd;">seq</span><span style="color: black;">&#91;</span>Natural<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">@</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> p <span style="color: #66cc66;">*</span> p <span style="color: #66cc66;">&gt;</span> n:</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">break</span> <span style="color: #808080; font-style: italic;"># 自己乘以自己都已经比上限的平方大了，那么自己啥也筛不到了，都被前面的因数筛完了</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#自己是上一轮的幸存者，这一轮没有改变numbers，所以不需要加入素数</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; primes.<span style="color: #dc143c;">add</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># 比自己小的都已经删掉或者进质数列表了，自己没被筛掉就是质数</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> i, v <span style="color: #ff7700;font-weight:bold;">in</span> numbers:</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> v <span style="color: #ff7700;font-weight:bold;">mod</span> p <span style="color: #66cc66;">!=</span> <span class="nu0">0</span>: <span style="color: #808080; font-style: italic;"># 不能被用来筛的质数整除，所以不知道是不是质数</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newNumbers.<span style="color: #dc143c;">add</span><span style="color: black;">&#40;</span>v<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># 接着筛</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># 否则就淘汰，下一次不用筛了（这好像是一种优化过的方法，相当于欧式筛法的效果？）</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; numbers <span style="color: #66cc66;">=</span> newNumbers</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #dc143c;">echo</span> <span style="color: #66cc66;">&amp;</span><span style="color: #483d8b;">&quot;numbers left: {numbers[0..min(10, high(numbers))]}... after sieved by {p}&quot;</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">primes <span style="color: #66cc66;">&amp;=</span> numbers <span style="color: #808080; font-style: italic;"># 把剩下的幸存者（一定是质数）捞上</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #dc143c;">echo</span> <span style="color: #483d8b;">&quot;---Result---&quot;</span></div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #dc143c;">echo</span> primes</div></li>
<li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
</ol></div>]]></content:encoded>
</item>
</channel>
</rss>
