﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-&lt;DIV id="ddm_Title"&gt;&lt;H1&gt;大大毛&amp;nbsp;&lt;i class="little"&gt;的笔记&lt;/i&gt;&lt;/H1&gt;&lt;H2&gt;&amp;nbsp&amp;nbsp;DDM's Note&lt;/H2&gt;&lt;/DIV&gt;-文章分类-&lt;b value="80" class="ddm_Item"&gt;常用算法&lt;/b&gt;</title><link>http://www.blogjava.net/tw-ddm/category/21198.html</link><description>&lt;div id="ddm_subTitle"&gt;
&lt;h2 class="catchline"&gt;
        哪怕没有办法一定有说法,&lt;br/&gt;
        就算没有鸽子一定有乌鸦,&lt;br/&gt;
        固执无罪&amp;nbsp;梦想有价,&lt;br/&gt;
        让他们惊讶.
&lt;/h2&gt;
&lt;/div&gt;</description><language>zh-cn</language><lastBuildDate>Thu, 26 Apr 2007 02:45:17 GMT</lastBuildDate><pubDate>Thu, 26 Apr 2007 02:45:17 GMT</pubDate><ttl>60</ttl><item><title>求最大公约数及最小公倍数</title><link>http://www.blogjava.net/tw-ddm/articles/108069.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Mon, 02 Apr 2007 14:55:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/108069.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/108069.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/108069.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/108069.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/108069.html</trackback:ping><description><![CDATA[
		<p>
				<br />
				<font face="Georgia" size="2">      最近用到最小公倍数、最大公约数是因为需要对用户 Key In 的数据（分数形式）进行检验，检验的规则是同组的数据之和 =  1，这样就需要求分母的最小公倍数。<br /><br />      在网上搜索了一下，主要介绍的算法有两种：</font>
		</p>
		<p>
				<font face="Georgia" size="2">      <strong>1. 欧几里德算法<br /></strong>         又叫辗转相除法，是最常用的算法，以前学到的就是这个了。它仅在对大素数的模运算时会遇到麻烦。<br /></font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #008000">'</span>
				<span style="COLOR: #008000">Euclid算法（VB）</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #0000ff">Public</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Function</span>
				<span style="COLOR: #000000"> fEuclid(lngNum1 </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">, lngNum2 </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">Dim</span>
				<span style="COLOR: #000000"> lngA </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">,lngB </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">,lngC </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000"> lngNum1 </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Or</span>
				<span style="COLOR: #000000"> lngNum2 </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Then</span>
				<span style="COLOR: #000000">
						<br />        </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000"> lngNum1 </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Then</span>
				<span style="COLOR: #000000">
						<br />            fEuclid </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngNum2<br />        </span>
				<span style="COLOR: #0000ff">Else</span>
				<span style="COLOR: #000000">
						<br />            fEuclid </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngNum1<br />        </span>
				<span style="COLOR: #0000ff">End</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000">
						<br />        </span>
				<span style="COLOR: #0000ff">Exit</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Function</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">End</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000"> lngNum1 </span>
				<span style="COLOR: #000000">&gt;=</span>
				<span style="COLOR: #000000"> lngNum2 </span>
				<span style="COLOR: #0000ff">Then</span>
				<span style="COLOR: #000000">
						<br />        lngA </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngNum1:lngB </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngNum2<br />    </span>
				<span style="COLOR: #0000ff">Else</span>
				<span style="COLOR: #000000">
						<br />        lngA </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngNum2:lngB </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngNum1<br />    </span>
				<span style="COLOR: #0000ff">End</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000">
						<br />    lngC </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngA </span>
				<span style="COLOR: #0000ff">Mod</span>
				<span style="COLOR: #000000"> lngB<br />    </span>
				<span style="COLOR: #0000ff">Do</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">While</span>
				<span style="COLOR: #000000"> lngC </span>
				<span style="COLOR: #000000">&gt;</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">
						<br />        lngA </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngB:lngB </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngC<br />        lngC </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngA </span>
				<span style="COLOR: #0000ff">Mod</span>
				<span style="COLOR: #000000"> lngB<br />    </span>
				<span style="COLOR: #0000ff">Loop</span>
				<span style="COLOR: #000000">
						<br />    <br />    fEuclid </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngB<br /><br /></span>
				<span style="COLOR: #0000ff">End Function</span>
		</div>
		<p>
				<font face="Georgia" size="2">      <strong>2. Stein算法</strong><br />         a.    如果A=0，B是最大公约数；如果B=0，A是最大公约数<br />         b.   A<sub>n</sub> = A，B<sub>n</sub> = B，C<sub>n</sub> = 1 （当n=1时）<br />         c1. 如果A<sub>n</sub>和B<sub>n</sub>都是偶数，则A<sub>n+1</sub> = A<sub>n</sub> /2，B<sub>n+1</sub> = B<sub>n</sub> /2，C<sub>n+1</sub> = C<sub>n</sub> *2<br />         c2. 如果A<sub>n</sub>是偶数，B<sub>n</sub>不是偶数，则A<sub>n+1</sub> = A<sub>n</sub> /2，B<sub>n+1</sub> = B<sub>n</sub> ，C<sub>n+1</sub> = C<sub>n</sub><br />         c3. 如果B<sub>n</sub>是偶数，A<sub>n</sub>不是偶数，则B<sub>n+1</sub> = B<sub>n</sub> /2，A<sub>n+1</sub> = A<sub>n</sub> ，C<sub>n+1</sub> = C<sub>n</sub><br />         c4. 如果A<sub>n</sub>和B<sub>n</sub>都不是偶数，则A<sub>n+1</sub> = |A<sub>n</sub> - B<sub>n</sub>|，B<sub>n+1</sub> =min(A<sub>n</sub>,B<sub>n</sub>)，C<sub>n+1</sub> = C<sub>n</sub><br />         d.    n++<br />         Stein算法最大的好处就是它只使用到了减法以及移位操作（与2运算），能够避免欧几里德算法运用到大数上所面临的问题。</font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #008000">'</span>
				<span style="COLOR: #008000">Stein算法（VB）</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #0000ff">Public</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Function</span>
				<span style="COLOR: #000000"> fStein(lngNum1 </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">, lngNum2 </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">Dim</span>
				<span style="COLOR: #000000"> lngResult </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">, lngA </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">, lngB </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">, lngC </span>
				<span style="COLOR: #0000ff">As</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Long</span>
				<span style="COLOR: #000000">
						<br />    lngA </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngNum1: lngB </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngNum2: lngC </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">
						<br />    <br />    </span>
				<span style="COLOR: #0000ff">Do</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">While</span>
				<span style="COLOR: #000000"> lngA </span>
				<span style="COLOR: #000000">&lt;&gt;</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">And</span>
				<span style="COLOR: #000000"> lngB </span>
				<span style="COLOR: #000000">&lt;&gt;</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">
						<br />        </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000"> lngA </span>
				<span style="COLOR: #0000ff">Mod</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">And</span>
				<span style="COLOR: #000000"> lngB </span>
				<span style="COLOR: #0000ff">Mod</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Then</span>
				<span style="COLOR: #000000">
						<br />            lngA </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngA </span>
				<span style="COLOR: #000000">/</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000">: lngB </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngB </span>
				<span style="COLOR: #000000">/</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000">: lngC </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngC </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000">
						<br />        </span>
				<span style="COLOR: #0000ff">Else</span>
				<span style="COLOR: #000000">
						<br />            </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000"> lngA </span>
				<span style="COLOR: #0000ff">Mod</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">&lt;&gt;</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">And</span>
				<span style="COLOR: #000000"> lngB </span>
				<span style="COLOR: #0000ff">Mod</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">&lt;&gt;</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Then</span>
				<span style="COLOR: #000000">
						<br />                </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000"> lngA </span>
				<span style="COLOR: #000000">&gt;</span>
				<span style="COLOR: #000000"> lngB </span>
				<span style="COLOR: #0000ff">Then</span>
				<span style="COLOR: #000000">
						<br />                    </span>
				<span style="COLOR: #008000">'</span>
				<span style="COLOR: #008000">lngB = lngB</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #000000">                    lngA </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngA </span>
				<span style="COLOR: #000000">-</span>
				<span style="COLOR: #000000"> lngB<br />                </span>
				<span style="COLOR: #0000ff">Else</span>
				<span style="COLOR: #000000">
						<br />                    lngB </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngA<br />                    lngA </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngB </span>
				<span style="COLOR: #000000">-</span>
				<span style="COLOR: #000000"> lngA<br />                </span>
				<span style="COLOR: #0000ff">End</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000">
						<br />            </span>
				<span style="COLOR: #0000ff">Else</span>
				<span style="COLOR: #000000">
						<br />                </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000"> lngA </span>
				<span style="COLOR: #0000ff">Mod</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Then</span>
				<span style="COLOR: #000000">
						<br />                    lngA </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngA </span>
				<span style="COLOR: #000000">/</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000">
						<br />                </span>
				<span style="COLOR: #0000ff">Else</span>
				<span style="COLOR: #000000">
						<br />                    lngB </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngB </span>
				<span style="COLOR: #000000">/</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">2</span>
				<span style="COLOR: #000000">
						<br />                </span>
				<span style="COLOR: #0000ff">End</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000">
						<br />            </span>
				<span style="COLOR: #0000ff">End</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000">
						<br />        </span>
				<span style="COLOR: #0000ff">End</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">Loop</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000"> lngA </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Then</span>
				<span style="COLOR: #000000">
						<br />        lngResult </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngC </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000"> lngB<br />    </span>
				<span style="COLOR: #0000ff">Else</span>
				<span style="COLOR: #000000">
						<br />        lngResult </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngC </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000"> lngA<br />    </span>
				<span style="COLOR: #0000ff">End</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">If</span>
				<span style="COLOR: #000000">
						<br />    <br />    fStein </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> lngResult<br /><br /></span>
				<span style="COLOR: #0000ff">End Function</span>
		</div>
		<p>
				<font face="Georgia" size="2">      <span style="FONT-SIZE: 10pt"><strong>最小公倍数 = M * N / 最大公约数</strong></span></font>
		</p>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/108069.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2007-04-02 22:55 <a href="http://www.blogjava.net/tw-ddm/articles/108069.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>