• <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>

            C++研究

            C++細節深度探索及軟件工程

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              37 隨筆 :: 0 文章 :: 74 評論 :: 0 Trackbacks
            A friend ask me 'How can you efficeny judge whether the num is primer? '

            There is a easy way to do it , the follow code isn't written by me  but  a classic method

            E.G quote from STL tutorial reference
            #include <iostream>
            #include 
            <list>
            #include 
            <algorithm>
            #include 
            <cstdlib> //for abs()
            using namespace std;
            //predicate, which returns whether an integer is a prime number
            bool isPrime (int number)
            {
            //ignore negative sign
            number = abs(number);
            // 0 and 1 are prime numbers
            if (number == 0 || number == 1{
            return true;
            }

            //find divisor that divides without a remainder
            int divisor;
            for (divisor = number/2; number%divisor != 0--divisor) {
            ;
            }

            //if no divisor greater than 1 is found, it is a prime number
            return divisor == 1;
            }


            posted on 2007-04-19 02:39 常興龍 閱讀(328) 評論(1)  編輯 收藏 引用 所屬分類: Algorithm

            評論

            # re: How can you efficeny judge whether the num is primer? 2008-07-05 16:31 我們一起來提高
            我的看法:
            (1)0和1都不算素數。
            (2)2和3都是素數,可以直接返回。
            (3)判斷一個比較小的數(可以認為在long范圍內的吧,如果是1024二進制位的大數就得想別的辦法了)是不是素數肯定得窮舉,提高效率就得根據已知的條件縮小窮舉的空間。我認為以上程序的窮舉空間還不夠小。
            其實只要對大于3的數num窮舉從2~sqrt(num)就夠了,而不用擴展到num/2。假如判斷1122是不是素數,只需要窮舉1122對2~33這個范圍的余數有沒有為0的就可以,超過33的數,如果正好有另一個積數,那么這個積數一定小于sqrt(num),乘法運算是對稱的,這是數實際上已經窮舉過了。這樣以1122為例就少窮舉了528次,效率提高了約17倍,而這個num越大,效率提高就越明顯。

            不知道我說的對不對,請大家指教啊。
              回復  更多評論
              

            > hi的博客
            午夜不卡888久久| 99久久久精品免费观看国产| 久久精品国产亚洲一区二区| 国产ww久久久久久久久久| 成人a毛片久久免费播放| 欧美大战日韩91综合一区婷婷久久青草 | 久久亚洲精品无码aⅴ大香| 99精品国产免费久久久久久下载| 久久99精品国产自在现线小黄鸭 | 久久国产精品免费一区| 久久久久久国产精品美女| 99久久人人爽亚洲精品美女| 久久久噜噜噜久久中文字幕色伊伊| 久久久久久亚洲精品成人| 欧洲国产伦久久久久久久| 久久精品国产清高在天天线| 亚洲&#228;v永久无码精品天堂久久 | 久久se这里只有精品| 色偷偷偷久久伊人大杳蕉| 午夜精品久久久久久| 久久精品国产亚洲沈樵| 欧洲成人午夜精品无码区久久| 日韩影院久久| 久久综合久久伊人| 久久精品女人天堂AV麻| 国内精品久久久久伊人av| 久久狠狠爱亚洲综合影院| 亚洲精品高清一二区久久| 大美女久久久久久j久久| 久久精品国产亚洲精品2020| 一本一本久久A久久综合精品| 久久嫩草影院免费看夜色| 国内精品久久久久久久coent | 国产精品中文久久久久久久| 久久久久久国产精品无码下载 | 亚洲综合伊人久久大杳蕉| 久久精品免费全国观看国产| 久久久免费观成人影院 | 精品久久久久久久| 久久久久亚洲AV成人片| 国产精品无码久久综合|