<?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>刘思喆 @ 贝吉塔行星 &#187; algorithms</title>
	<atom:link href="http://www.bjt.name/tag/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bjt.name</link>
	<description>R 语言，数据挖掘，数据可视化</description>
	<lastBuildDate>Wed, 30 Nov 2011 15:43:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>一个美丽的三角形</title>
		<link>http://www.bjt.name/2011/06/beautiful-math-triangle-r/</link>
		<comments>http://www.bjt.name/2011/06/beautiful-math-triangle-r/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 15:56:09 +0000</pubDate>
		<dc:creator>刘思喆</dc:creator>
				<category><![CDATA[技巧随笔]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[R 语言]]></category>

		<guid isPermaLink="false">http://www.bjt.name/?p=10812</guid>
		<description><![CDATA[yanlinlin 站长放出个问题，按照《魔方与数学》一文的后记上记录的： 请将1、11、111、1111、……的平方写下来，从最小的开始，每个占一行，并且全部居中书写，图形的样子？ 问题不难，不外乎就是将这些数字平方，在按照要求放置。但，存在一个小问题，计算机一个单元存储数据的数据长度不是无限制的，而且精度也有所限制，所以当11111111平方的时候，R给出的数据已经是约数了。帖子讨论详见（这里） 还好，这些数据的平方比较简单，可以通过模拟平方的运算过程来得到精确的结果，代码如下： 生成的图形有些像杨辉三角，也是非常漂亮： 但我想应该还有更好的实现方式，期待跟帖出现。 相关文章： 数据挖掘之R与SQL 2006年至今国内货币供应量的Motion Chart 用R实现马赛克拼图]]></description>
			<content:encoded><![CDATA[<p>yanlinlin 站长放出个问题，按照<a href="http://blog.sciencenet.cn/home.php?mod=space&amp;uid=576779&amp;do=blog&amp;id=456080">《魔方与数学》</a>一文的后记上记录的：</p>
<blockquote><p>请将<span style="font-family: Calibri;">1</span>、<span style="font-family: Calibri;">11</span>、<span style="font-family: Calibri;">111</span>、<span style="font-family: Calibri;">1111</span>、……的平方写下来，从最小的开始，每个占一行，并且全部居中书写，图形的样子？</p></blockquote>
<p>问题不难，不外乎就是将这些数字平方，在按照要求放置。但，存在一个小问题，计算机一个单元存储数据的数据长度不是无限制的，而且精度也有所限制，所以当11111111平方的时候，R给出的数据已经是约数了。帖子讨论详见（<a href="http://cos.name/cn/topic/104640" target="_blank">这里</a>）</p>
<p>还好，这些数据的平方比较简单，可以通过模拟平方的运算过程来得到精确的结果，代码如下：</p>
<p><a href="http://www.bjt.name/wp-content/uploads/2011/06/code.jpg"><img class="aligncenter size-full wp-image-10813" title="code" src="http://www.bjt.name/wp-content/uploads/2011/06/code.jpg" alt="" width="387" height="381" /></a>生成的图形有些像<a href="http://zh.wikipedia.org/wiki/%E6%9D%A8%E8%BE%89%E4%B8%89%E8%A7%92%E5%BD%A2" target="_blank">杨辉三角</a>，也是非常漂亮：</p>
<p><a href="http://www.bjt.name/wp-content/uploads/2011/06/111.jpg"><img class="aligncenter size-full wp-image-10814" title="111" src="http://www.bjt.name/wp-content/uploads/2011/06/111.jpg" alt="" width="498" height="493" /></a></p>
<p>但我想应该还有更好的实现方式，期待跟帖出现。<br />
<h3 class='related_post_title'>相关文章：</h3>
<ul class='related_post'>
<li><a href='http://www.bjt.name/2011/08/r-and-sql-datamining/' title='数据挖掘之R与SQL'>数据挖掘之R与SQL</a></li>
<li><a href='http://www.bjt.name/2011/07/money-supply-motion-char/' title='2006年至今国内货币供应量的Motion Chart'>2006年至今国内货币供应量的Motion Chart</a></li>
<li><a href='http://www.bjt.name/2011/06/mosaic-plot-with-r/' title='用R实现马赛克拼图'>用R实现马赛克拼图</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.bjt.name/2011/06/beautiful-math-triangle-r/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>素数的求法</title>
		<link>http://www.bjt.name/2009/06/prime-numbers-r/</link>
		<comments>http://www.bjt.name/2009/06/prime-numbers-r/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 15:50:31 +0000</pubDate>
		<dc:creator>刘思喆</dc:creator>
				<category><![CDATA[R 语言]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[prime]]></category>
		<category><![CDATA[素数]]></category>

		<guid isPermaLink="false">http://www.bjt.name/2009/06/%e7%b4%a0%e6%95%b0%e7%9a%84%e6%b1%82%e6%b3%95/</guid>
		<description><![CDATA[COS 上有人问过如何求1～100的素数。虽说这个问题没准就是计算机系大一新生的一道作业题，但对我这个几乎没有任何 C 编程经验的人来说，似乎还是有些挑战。花了几分钟整理了一下思路，既然素数的定义是只能被1和自身整除，那么： 1、如果第 n 个数，不能它前面的所有的数字（不包括1）整除，那么即为定义。但需要遍历所有数字，效率肯定不好。 2、如果 n 不能被 n 前面的所有素数整除的话，那么 n 应该是下一个素数（后来知道这个是算术基本定理）。 根据第二点思路，写出 R 代码： prime2 &#60;- function(m){ x &#60;- c(2,3,5,7,11) for(i in 13:m){ if(!any(i%%x == 0)) x &#60;- c(x,i) } return(x) } 即给出前 5 个素数，而后寻找第 6 个素数；再根据 6 个素数找第 7 个；类推……；直至 n。 马上 cloud_wei 给出了另外的实现方式：glm包的 isprime 函数（这个包似乎没有 Windows 版本）； 接着 jo3vul31l3 在回帖中给了最优的解法，即埃拉托斯特尼筛法： <a href='http://www.bjt.name/2009/06/prime-numbers-r/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>COS 上有人问过如何<a href="http://cos.name/bbs/read.php?tid=12781">求1～100的素数</a>。虽说这个问题没准就是计算机系大一新生的一道作业题，但对我这个几乎没有任何 C 编程经验的人来说，似乎还是有些挑战。花了几分钟整理了一下思路，既然素数的定义是只能被1和自身整除，那么： 1、如果第 n 个数，不能它前面的所有的数字（不包括1）整除，那么即为定义。但需要遍历所有数字，效率肯定不好。 2、如果 n 不能被 n 前面的所有素数整除的话，那么 n 应该是下一个素数（后来知道这个是<a title="算术基本定理" href="http://zh.wikipedia.org/w/index.php?title=算术基本定理&amp;variant=zh-cn">算术基本定理</a>）。 根据第二点思路，写出 R 代码：</p>
<pre>prime2 &lt;- function(m){
    x &lt;- c(2,3,5,7,11)
    for(i in 13:m){
        if(!any(i%%x == 0)) x &lt;- c(x,i)
    }
    return(x)
}</pre>
<p>即给出前 5 个素数，而后寻找第 6 个素数；再根据 6 个素数找第 7 个；类推……；直至 n。 马上 <strong><a href="http://cos.name/bbs/u.php?action=show&amp;uid=13526" target="_blank">cloud_wei</a></strong> 给出了另外的实现方式：glm包的 isprime 函数（这个包似乎没有 Windows 版本）； 接着 <a href="http://cos.name/bbs/u.php?action=show&amp;uid=101557" target="_blank"><strong>jo3vul31l3</strong></a> 在回帖中给了最优的解法，即<a title="埃拉托斯特尼筛法" href="http://zh.wikipedia.org/w/index.php?title=埃拉托斯特尼筛法&amp;variant=zh-cn">埃拉托斯特尼筛法</a>：</p>
<pre>prime3&lt;-function(m){
    x&lt;-c(2:m) ; y&lt;-NULL
    repeat{
        z&lt;-x[(x%%x[1])!=0] ; y&lt;-c(y,x[1])
        if(x[1]&gt;sqrt(m))break
        x&lt;-z
    }
    c(y,z)
}</pre>
<p>在这以前我一直以为 素数 越到后面会越”稀疏”，但事实是这样（1：10000区间的素数）：</p>
<p style="text-align: center;"><a href="http://www.bjt.name/wp-content/uploads/2009/06/prime.png"><img class="size-full wp-image-10584 aligncenter" title="prime" src="http://www.bjt.name/wp-content/uploads/2009/06/prime.png" alt="" width="614" height="283" /></a></p>
<p>第一幅图的红色线是顺手做的一个线性回归拟合线；10000以前的素数几乎是平均的出现在各个区间（breaks = 100）。<br />
<h3 class='related_post_title'>相关文章：</h3>
<ul class='related_post'>
<li><a href='http://www.bjt.name/2011/06/beautiful-math-triangle-r/' title='一个美丽的三角形'>一个美丽的三角形</a></li>
<li><a href='http://www.bjt.name/2008/10/quantile-nine-algorithms/' title='quantile 的 9 中求法'>quantile 的 9 中求法</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.bjt.name/2009/06/prime-numbers-r/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>quantile 的 9 中求法</title>
		<link>http://www.bjt.name/2008/10/quantile-nine-algorithms/</link>
		<comments>http://www.bjt.name/2008/10/quantile-nine-algorithms/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 06:23:00 +0000</pubDate>
		<dc:creator>刘思喆</dc:creator>
				<category><![CDATA[R 语言]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[quantile]]></category>

		<guid isPermaLink="false">http://www.bjt.name/archives/9977</guid>
		<description><![CDATA[逛了逛 谢 的blog，被《Inverse Empirical Cumulative Distribution》这篇文章吸引过去&#8230;&#8230; 确实当学习 stat 这么多年第一次知道 quantile 有 9种求法，诧异心情完全理解。记得写 R_FAQ_cn时，关于 fivenum 和 summary 函数区别时，翻阅 quantile 函数发现了这个问题，当时自己都想呕血了。 说实话，这个问题稍好整理下，在国内发到顶级统计期刊上都不为过，有兴趣的xdjm可以考虑下。 &#160; 相关文章： 一个美丽的三角形 素数的求法]]></description>
			<content:encoded><![CDATA[<p>逛了逛 谢 的blog，被《Inverse Empirical Cumulative Distribution》这篇文章吸引过去&hellip;&hellip;</p>
<p>确实当学习 stat 这么多年第一次知道 quantile 有 9种求法，诧异心情完全理解。记得写 R_FAQ_cn时，关于 fivenum 和 summary 函数区别时，翻阅 quantile 函数发现了这个问题，当时自己都想呕血了。<img alt="书呆子" src="http://shared.live.com/HjKMzTS-xzcms40!CabizA/emoticons/smile_nerd.gif" title="书呆子" style="vertical-align: middle;" /></p>
<p>说实话，这个问题稍好整理下，在国内发到顶级统计期刊上都不为过，有兴趣的xdjm可以考虑下。</p>
<p>&nbsp;</p>
<h3 class='related_post_title'>相关文章：</h3>
<ul class='related_post'>
<li><a href='http://www.bjt.name/2011/06/beautiful-math-triangle-r/' title='一个美丽的三角形'>一个美丽的三角形</a></li>
<li><a href='http://www.bjt.name/2009/06/prime-numbers-r/' title='素数的求法'>素数的求法</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.bjt.name/2008/10/quantile-nine-algorithms/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

