• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            XGuru's Blog

            技術(shù),是一種態(tài)度。關(guān)注:高性能后端技術(shù)/服務(wù)器架構(gòu)/C++/C/LAMP

               :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              20 Posts :: 0 Stories :: 93 Comments :: 0 Trackbacks

            公告





            twitter / xoXGuru

            feedsky
            抓虾
            google reader
            鲜果
            QQ邮箱
            九点

            常用鏈接

            留言簿(12)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            by Xguru

             又說(shuō)階乘,這是老生常談了吧。想都不用想,一個(gè)遞歸輕松搞定!

            int factorial(int n)
            {
                
            if( n == 1)
                    
            return 1;
                
            return n * factorial(n-1);
            }

            或者你覺(jué)得遞歸效率沒(méi)有尾遞歸來(lái)的好 ,大筆一揮。

            long fact_iter(long product, long counter, long maxcount) 
            {      
              
            return (counter > maxcount) ? product : fact_iter(product*counter, counter+1, maxcount);
            }
                  

            long factorial(long n) 
            {
              
            return fact_iter(11, n);    
            }

             
                     或者你看過(guò)《代碼大全》上面說(shuō)過(guò):“如果為我工作的程序員用遞歸去計(jì)算階乘那么我寧愿換人。”
            使用遞歸求階乘速度緩慢,無(wú)法預(yù)測(cè)運(yùn)行期間內(nèi)存使用情況,難以理解。于是你把遞歸改成了循環(huán)語(yǔ)句。

            int factorial(int n)
            {
                
            int result = 1;
                
            for(int i = 2 ; i <= n; i++)
                
            {
                    result 
            = result * i;
                }

                
            return result;
            }
                      當(dāng)你寫(xiě)下這些代碼的時(shí)候,會(huì)不會(huì)覺(jué)得少了些什么?

                      在我的32位環(huán)境上測(cè)試一下,計(jì)算到33!的時(shí)候的溢出了,于是你會(huì)說(shuō),這是int的值太小了嘛,于是你換了個(gè)long double,測(cè)試一下,什么玩意嘛這是,數(shù)再大一點(diǎn)的話也不行了

                     
             那就改用鏈表或者數(shù)組表示吧,鏈表的速度就太慢了,用數(shù)組吧。

            int factorial2(int n,int a[])

                
            int carry;
                
            int digit = 1;
                a[
            0= 1;
                
            int temp;
                
            for(int i = 2; i <= n; ++i)
                
            {
                    
            for(int j = 1, carry = 0; j <= digit; ++j)    
                    
            {
                        temp 
            = a[j-1* i + carry;
                        a[j
            -1= temp % 10;
                        carry 
            = temp / 10;
                    }

                    
            while(carry) 
                    
            {
                        a[
            ++digit-1= carry % 10;
                        carry 
            /= 10;
                    }

                }

                
            return --digit;
            }

                    這個(gè)算法模擬手工計(jì)算的過(guò)程,將結(jié)果保存在a數(shù)組中,返回的是結(jié)果的位數(shù)

                   你在這個(gè)時(shí)候是不是感覺(jué)輕飄飄了呢?請(qǐng)暫時(shí)打住。

                    如果我要求一個(gè)
            10W以上大數(shù)的一個(gè)科學(xué)計(jì)數(shù)法的表達(dá)式呢?或者是問(wèn)你,10W 級(jí)別上的N!左邊第三位的數(shù)字是多少?呃,這個(gè)是數(shù)學(xué)家的事了吧?振作精神,來(lái)挑戰(zhàn)自我吧!真正的程序員需要的就是這種追根究底的精神。
                    來(lái)試試數(shù)學(xué)分析方法,James Stirling這位蘇格蘭數(shù)學(xué)家,280多年前就給出了這個(gè)極限式子

                                                 

                   這個(gè)式子能用極快的速度求出n!的近似值,也可以使用它來(lái)無(wú)限接近準(zhǔn)確結(jié)果。具體的介紹和證明過(guò)程在這里 或者 這里

            斯特靈級(jí)數(shù)公式



                  下面的代碼是求大數(shù)N!科學(xué)計(jì)數(shù)法表示

            struct bigNum 
            {
                   
            double n; //尾數(shù)
                   int    e; //指數(shù)
            }
            ;
            void factorial3(struct bigNum *p,int n)
            {
                   
            double logx,s,item;//s:級(jí)數(shù)的和  item:級(jí)數(shù)的每一項(xiàng)
                   int i;
                   logx 
            = n* log10((double)n/E);
                   p
            ->= (int)(logx);   p->n= pow(10.0, logx-p->e);
                   p
            ->*= sqrt( 2* PI* (double)n);
                   
            for (item=1.0,s=0.0,i=0;i<sizeof(a1)/sizeof(double);i++)
                   
            {
                          s 
            += item * a1[i];
                          item 
            /= (double)n;
                   }

                   p
            ->*=s;
            }

             

                  下面這個(gè)是階乘的對(duì)數(shù)的漸近展開(kāi)式
                           

            void factorial3b(struct bigNum *p,int n)
            {
                
            double logR;
                
            double s,item;
                
            int i;
                logR
            =0.5*log(2.0*PI)+((double)n+0.5)*log(n)-(double)n;
                
                
            for (item=1/(double)n,s=0.0,i=0;i<sizeof(a2)/sizeof(double);i++)
                
            {
                    s
            += item * a2[i];
                    item 
            /= (double)(n)* (double)n; 
                }

                logR
            +=s;
                p
            ->= (int)( logR / log(10));//換底公式
                p->= pow(10.00, logR/log(10- p->e);
            }

                   要是求階層的位數(shù)也是特別簡(jiǎn)單

            double getFactorialLength(int n)
            {
                
            return (n * log(double(n)) - n + 0.5 * log(2.0 * n * PI )) / log(10.0)+1;
            }

                   這個(gè)求出來(lái)的是位數(shù)的近似數(shù),或者是改進(jìn)一下,使用ceil函數(shù)來(lái)求出不小于給定實(shí)數(shù)的最小整數(shù)。

            int getFactorialLength(int n)
            {
                
            if( n == 1 )
                    
            return 1;
                
            else
                
            return (int)ceil((N*log(N)-N+log(2*N*PI)/2)/log(10));
            }


            到此,你會(huì)不由感嘆:計(jì)算機(jī)科學(xué)中最閃光,最精髓,最本質(zhì)的東西還是數(shù)學(xué)



            芒德布羅集合的邊界
            最后用羅素的話結(jié)束這篇隨筆:
                   Mathematics, rightly viewed, possesses not only truth, but supreme beauty — a beauty cold and austere, like that of sculpture, without appeal to any part of our weaker nature, without the gorgeous trappings of painting or music, yet sublimely pure, and capable of a stern perfection such as only the greatest art can show. The true spirit of delight, the exaltation, the sense of being more than Man, which is the touchstone of the highest excellence, is to be found in mathematics as surely as poetry.

             

            參考資料
            1.Tom M. Apostol.《數(shù)學(xué)分析, 微積分》(Mathematical Analysis)
            2.Steve McConnell.《代碼大全(第二版)》(CODE COMPLETE, Second Edition)
            3.http://en.wikipedia.org/wiki/Stirling_approximation#History

            4.
            http://mathworld.wolfram.com/StirlingsApproximation.html

            5.
            http://zh.straightworldbank.com/wiki/%E6%96%AF%E7%89%B9%E6%9E%97%E5%85%AC%E5%BC%8F

             

            posted on 2009-12-30 19:02 XGuru 閱讀(1903) 評(píng)論(4)  編輯 收藏 引用

            Feedback

            # re: 雜感系列之二--階乘算法雜感 2009-12-30 22:55 NighCrawler
            最后的那張分形幾何圖是軟件生成的?  回復(fù)  更多評(píng)論
              

            # re: 雜感系列之二--階乘算法雜感 2010-01-05 18:24 argmax
            Stirling公式只是一個(gè)近似,當(dāng)n較大時(shí)才比較接近原始的結(jié)果。  回復(fù)  更多評(píng)論
              

            # re: 雜感系列之二--階乘算法雜感[未登錄](méi) 2010-04-02 17:34 Mingle
            文章中的數(shù)學(xué)公式是如何書(shū)寫(xiě)的?  回復(fù)  更多評(píng)論
              

            # re: 雜感系列之二--階乘算法雜感[未登錄](méi) 2010-04-06 22:37 xguru
            @Mingle
            latex公式常用宏包 http://www.ctex.org/documents/packages/math/index.htm

            還有 word2007的公式生成也不錯(cuò)呢
              回復(fù)  更多評(píng)論
              


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            久久午夜无码鲁丝片| 久久国产V一级毛多内射| 亚洲精品美女久久777777| 三级韩国一区久久二区综合| 91精品国产综合久久香蕉| 久久精品国产72国产精福利| 亚洲国产综合久久天堂| 丰满少妇人妻久久久久久| 狠狠人妻久久久久久综合| 久久精品国产免费观看| 99久久成人18免费网站| 麻豆亚洲AV永久无码精品久久| 99久久免费国产精品| 漂亮人妻被中出中文字幕久久| 久久精品www| 亚洲AV乱码久久精品蜜桃| 久久se这里只有精品| 国产人久久人人人人爽| 日本久久中文字幕| 久久青青草原国产精品免费| 精品伊人久久久| 久久久久久久综合综合狠狠| 2020久久精品国产免费| 国内精品伊人久久久久777| 久久精品国产只有精品66| 亚洲一区中文字幕久久| 国产午夜精品久久久久免费视| 久久久久久久久波多野高潮| 久久精品一区二区三区中文字幕 | 久久精品无码专区免费| 狠狠狠色丁香婷婷综合久久俺| 2021国内久久精品| 奇米影视7777久久精品人人爽 | 国内精品伊人久久久久777| 久久午夜综合久久| 久久综合九色综合欧美就去吻| 国内精品久久久久久久久电影网| 久久九九亚洲精品| 99久久亚洲综合精品网站| 伊人色综合久久| 国产成人AV综合久久|