发了一个小小软件,"快速分解质因数 ",http://www.kehui.net/index.php/download/read/1/1109
有个问题想请大家给予帮助:使用BYTE 数组存放下列数据(比如1-10000内的素数),用一个过程可以读取并显示,如何能做到运行最快,代码最短,数组最小?
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97  101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423 2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939 2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 3581 3583 3593 3607 3613 3617 3623 3631 3637 3643 3659 3671 3673 3677 3691 3697 3701 3709 3719 3727 3733 3739 3761 3767 3769 3779 3793 3797 3803 3821 3823 3833 3847 3851 3853 3863 3877 3881 3889 3907 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 4001 4003 4007 4013 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 4099 4111 4127 4129 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 4241 4243 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349 4357 4363 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493 4507 4513 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621 4637 4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729 4733 4751 4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871 4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973 4987 4993 4999 5003 5009 5011 5021 5023 5039 5051 5059 5077 5081 5087 5099 5101 5107 5113 5119 5147 5153 5167 5171 5179 5189 5197 5209 5227 5231 5233 5237 5261 5273 5279 5281 5297 5303 5309 5323 5333 5347 5351 5381 5387 5393 5399 5407 5413 5417 5419 5431 5437 5441 5443 5449 5471 5477 5479 5483 5501 5503 5507 5519 5521 5527 5531 5557 5563 5569 5573 5581 5591 5623 5639 5641 5647 5651 5653 5657 5659 5669 5683 5689 5693 5701 5711 5717 5737 5741 5743 5749 5779 5783 5791 5801 5807 5813 5821 5827 5839 5843 5849 5851 5857 5861 5867 5869 5879 5881 5897 5903 5923 5927 5939 5953 5981 5987 6007 6011 6029 6037 6043 6047 6053 6067 6073 6079 6089 6091 6101 6113 6121 6131 6133 6143 6151 6163 6173 6197 6199 6203 6211 6217 6221 6229 6247 6257 6263 6269 6271 6277 6287 6299 6301 6311 6317 6323 6329 6337 6343 6353 6359 6361 6367 6373 6379 6389 6397 6421 6427 6449 6451 6469 6473 6481 6491 6521 6529 6547 6551 6553 6563 6569 6571 6577 6581 6599 6607 6619 6637 6653 6659 6661 6673 6679 6689 6691 6701 6703 6709 6719 6733 6737 6761 6763 6779 6781 6791 6793 6803 6823 6827 6829 6833 6841 6857 6863 6869 6871 6883 6899 6907 6911 6917 6947 6949 6959 6961 6967 6971 6977 6983 6991 6997 7001 7013 7019 7027 7039 7043 7057 7069 7079 7103 7109 7121 7127 7129 7151 7159 7177 7187 7193 7207 7211 7213 7219 7229 7237 7243 7247 7253 7283 7297 7307 7309 7321 7331 7333 7349 7351 7369 7393 7411 7417 7433 7451 7457 7459 7477 7481 7487 7489 7499 7507 7517 7523 7529 7537 7541 7547 7549 7559 7561 7573 7577 7583 7589 7591 7603 7607 7621 7639 7643 7649 7669 7673 7681 7687 7691 7699 7703 7717 7723 7727 7741 7753 7757 7759 7789 7793 7817 7823 7829 7841 7853 7867 7873 7877 7879 7883 7901 7907 7919 7927 7933 7937 7949 7951 7963 7993 8009 8011 8017 8039 8053 8059 8069 8081 8087 8089 8093 8101 8111 8117 8123 8147 8161 8167 8171 8179 8191 8209 8219 8221 8231 8233 8237 8243 8263 8269 8273 8287 8291 8293 8297 8311 8317 8329 8353 8363 8369 8377 8387 8389 8419 8423 8429 8431 8443 8447 8461 8467 8501 8513 8521 8527 8537 8539 8543 8563 8573 8581 8597 8599 8609 8623 8627 8629 8641 8647 8663 8669 8677 8681 8689 8693 8699 8707 8713 8719 8731 8737 8741 8747 8753 8761 8779 8783 8803 8807 8819 8821 8831 8837 8839 8849 8861 8863 8867 8887 8893 8923 8929 8933 8941 8951 8963 8969 8971 8999 9001 9007 9011 9013 9029 9041 9043 9049 9059 9067 9091 9103 9109 9127 9133 9137 9151 9157 9161 9173 9181 9187 9199 9203 9209 9221 9227 9239 9241 9257 9277 9281 9283 9293 9311 9319 9323 9337 9341 9343 9349 9371 9377 9391 9397 9403 9413 9419 9421 9431 9433 9437 9439 9461 9463 9467 9473 9479 9491 9497 9511 9521 9533 9539 9547 9551 9587 9601 9613 9619 9623 9629 9631 9643 9649 9661 9677 9679 9689 9697 9719 9721 9733 9739 9743 9749 9767 9769 9781 9787 9791 9803 9811 9817 9829 9833 9839 9851 9857 9859 9871 9883 9887 9901 9907 9923 9929 9931 9941 9949 9967 9973

解决方案 »

  1.   

    仅以你提供的数据分析结果:1、Abs(A(i)-A(i-1))取改变量。下面是改变量数据。
    1 2 2 4 2 4 2 4 6 2 6 4 2 4 6 6 2 6 4 2 6 4 6 8 4 2 4 2 4 14 4 6 2 10 2 6 6 4 6 6 2 10 2 4 2 12 12 4 2 4 6 2 10 6 6 6 2 6 4 2 10 14 4 2 4 14 6 10 2 4 6 8 6 6 4 6 8 4 8 10 2 10 2 6 4 6 8 4 2 4 12 8 4 8 4 6 12 2 18 6 10 6 6 2 6 10 6 6 2 6 6 4 2 12 10 2 4 6 6 2 12 4 6 8 10 8 10 8 6 6 4 8 6 4 8 4 14 10 12 2 10 2 4 2 10 14 4 2 4 14 4 2 4 20 4 8 10 8 4 6 6 14 4 6 6 8 6 12 4 6 2 10 2 6 10 2 10 2 6 18 4 2 4 6 6 8 6 6 22 2 10 8 10 6 6 8 12 4 6 6 2 6 12 10 18 2 4 6 2 6 4 2 4 12 2 6 34 6 6 8 18 10 14 4 2 4 6 8 4 2 6 12 10 2 4 2 4 6 12 12 8 12 6 4 6 8 4 8 4 14 4 6 2 4 6 2 6 10 20 6 4 2 24 4 2 10 12 2 10 8 6 6 6 18 6 4 2 12 10 12 8 16 14 6 4 2 4 2 10 12 6 6 18 2 16 2 22 6 8 6 4 2 4 8 6 10 2 10 14 10 6 12 2 4 2 10 12 2 16 2 6 4 2 10 8 18 24 4 6 8 16 2 4 8 16 2 4 8 6 6 4 12 2 22 6 2 6 4 6 14 6 4 2 6 4 6 12 6 6 14 4 6 12 8 6 4 26 18 10 8 4 6 2 6 22 12 2 16 8 4 12 14 10 2 4 8 6 6 4 2 4 6 8 4 2 6 10 2 10 8 4 14 10 12 2 6 4 2 16 14 4 6 8 6 4 18 8 10 6 6 8 10 12 14 4 6 6 2 28 2 10 8 4 14 4 8 12 6 12 4 6 20 10 2 16 26 4 2 12 6 4 12 6 8 4 8 22 2 4 2 12 28 2 6 6 6 4 6 2 12 4 12 2 10 2 16 2 16 6 20 16 8 4 2 4 2 22 8 12 6 10 2 4 6 2 6 10 2 12 10 2 10 14 6 4 6 8 6 6 16 12 2 4 14 6 4 8 10 8 6 6 22 6 2 10 14 4 6 18 2 10 14 4 2 10 14 4 8 18 4 6 2 4 6 2 12 4 20 22 12 2 4 6 6 2 6 22 2 6 16 6 12 2 6 12 16 2 4 6 14 4 2 18 24 10 6 2 10 2 10 2 10 6 2 10 2 10 6 8 30 10 2 10 8 6 10 18 6 12 12 2 18 6 4 6 6 18 2 10 14 6 4 2 4 24 2 12 6 16 8 6 6 18 16 2 4 6 2 6 6 10 6 12 12 18 2 6 4 18 8 24 4 2 4 6 2 12 4 14 30 10 6 12 14 6 10 12 2 4 6 8 6 10 2 4 14 6 6 4 6 2 10 2 16 12 8 18 4 6 12 2 6 6 6 28 6 14 4 8 10 8 12 18 4 2 4 24 12 6 2 16 6 6 14 10 14 4 30 6 6 6 8 6 4 2 12 6 4 2 6 22 6 2 4 18 2 4 12 2 6 4 26 6 6 4 8 10 32 16 2 6 4 2 4 2 10 14 6 4 8 10 6 20 4 2 6 30 4 8 10 6 6 8 6 12 4 6 2 6 4 6 2 10 2 16 6 20 4 12 14 28 6 20 4 18 8 6 4 6 14 6 6 10 2 10 12 8 10 2 10 8 12 10 24 2 4 8 6 4 8 18 10 6 6 2 6 10 12 2 10 6 6 6 8 6 10 6 2 6 6 6 10 8 24 6 22 2 18 4 8 10 30 8 18 4 2 10 6 2 6 4 18 8 12 18 16 6 2 12 6 10 2 10 2 6 10 14 4 24 2 16 2 10 2 10 20 4 2 4 8 16 6 6 2 12 16 8 4 6 30 2 10 2 6 4 6 6 8 6 4 12 6 8 12 4 14 12 10 24 6 12 6 2 22 8 18 10 6 14 4 2 6 10 8 6 4 6 30 14 10 2 12 10 2 16 2 18 24 18 6 16 18 6 2 18 4 6 2 10 8 10 6 6 8 4 6 2 10 2 12 4 6 6 2 12 4 14 18 4 6 20 4 8 6 4 8 4 14 6 4 14 12 4 2 30 4 24 6 6 12 12 14 6 4 2 4 18 6 12 8 6 4 12 2 12 30 16 2 6 22 14 6 10 12 6 2 4 8 10 6 6 24 14 6 4 8 12 18 10 2 10 2 4 6 20 6 4 14 4 2 4 14 6 12 24 10 6 8 10 2 30 4 6 2 12 4 14 6 34 12 8 6 10 2 4 20 10 8 16 2 10 14 4 2 12 6 16 6 8 4 8 4 6 8 6 6 12 6 4 6 6 8 18 4 20 4 12 2 10 6 2 10 12 2 4 20 6 30 6 4 8 10 12 6 2 28 2 6 4 2 16 12 2 6 10 8 24 12 6 18 6 4 14 6 4 12 8 6 12 4 6 12 6 12 2 16 20 4 2 10 18 8 4 14 4 2 6 22 6 14 6 6 10 6 2 10 2 4 2 22 2 4 6 6 12 6 14 10 12 6 8 4 36 14 12 6 4 6 2 12 6 12 16 2 10 8 22 2 12 6 4 6 18 2 12 6 4 12 8 6 12 4 6 12 6 2 12 12 4 14 6 16 6 2 10 8 18 62、对改变量取频率。下面是频率表(0-255)。
    0 1 205 0 202 0 299 0 101 0 119 0 105 0 54 0 33 0 40 0 15 0 16 0 15 0 3 0 5 0 11 0 1 0 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 03、根据频率确定编码表。
    编码 数值(改变量) 频率
    0  6  299
    1  2  205
    2  4  202
    3  10  119
    4  12  105
    5  8  101
    6  14  54
    7  18  40
    8  16  33
    9  22  16
    A  20  15
    B  24  15
    C  30  11
    D  28  5
    E  26  3
    (F以下用一个字节,F以上用1/2字节)
    F0  34  2
    F1  1  1
    F2  32  1
    F3  36  1算法是这样的:
    1、取改变量。
    2、取改变量频率。
    3、根据改变量频率确定编码表。
    4、将改变量数据按4Bit编码,保存在中间数组里(就是用8Bit对应一个4bit,这样做是为了计算方便)。
    5、将4Bit的Bytes数组转换为真正的Bytes数组。大约700个字节就够了。
      

  2.   

    to  KiteGirl(小仙妹) 和你上面提供的数据绝对类似吗?如果是这样的话,我有办法!
    -------------------
    以上只是个例子,小仙妹是跳蚤算法的高手,说说看。
    其实我的目的是把1亿范围内的素数存入一个数组。
    对于分解质因数,帮一个小朋友写的小程序:
    http://www.kehui.net/index.php/download/read/1/1109 
    自我感觉速度还可以,就是程序大了点,所以提出这个问题。万分感谢。
      

  3.   

    小仙妹比我手快,你的代码,很有启发,多谢。
    我的想法是把素数分成8种情况:30n+1,30n+7,30n+11,30n+13,30n+17,30n+19,30n+23,30n+29 对于每一个n,把对应的数是否素数写为0 or 1,按8 bit 写入BYTE,但这样读取不太方便,不知可行不行?
      

  4.   

    如果直接在获得频率后将19个值对应19个字母的话,可以获得如下编码。这个办法虽然不如上面的方法可以利用半个字节。但这个编码容易编辑、录入,因为都是字母。ABBCBCBCDBDCBCDDBDCBDCDECBCBCFCDBGBDDCDDBGBCBHHCBCDBGDDDBDCBGFCBCFDGBCDEDDCDECEGBGBDCDECBCHECECDHBIDGDDBDGDDBDDCBHGBCDDBHCDEGEGEDDCEDCECFGHBGBCBGFCBCFCBCJCEGECDDFCDDEDHCDBGBDGBGBDICBCDDEDDKBGEGDDEHCDDBDHGIBCDBDCBCHBDLDDEIGFCBCDECBDHGBCBCDHHEHDCDECECFCDBCDBDGJDCBMCBGHBGEDDDIDCBHGHENFDCBCBGHDDIBNBKDEDCBCEDGBGFGDHBCBGHBNBDCBGEIMCDENBCENBCEDDCHBKDBDCDFDCBDCDHDDFCDHEDCOIGECDBDKHBNECHFGBCEDDCBCDECBDGBGECFGHBDCBNFCDEDCIEGDDEGHFCDDBPBGECFCEHDHCDJGBNOCBHDCHDECEKBCBHPBDDDCDBHCHBGBNBNDJNECBCBKEHDGBCDBDGBHGBGFDCDEDDNHBCFDCEGEDDKDBGFCDIBGFCBGFCEICDBCDBHCJKHBCDDBDKBDNDHBDHNBCDFCBIMGDBGBGBGDBGBGDEQGBGEDGIDHHBIDCDDIBGFDCBCMBHDNEDDINBCDBDDGDHHIBDCIEMCBCDBHCFQGDHFDGHBCDEDGBCFDDCDBGBNHEICDHBDDDPDFCEGEHICBCMHDBNDDFGFCQDDDEDCBHDCBDKDBCIBCHBDCODDCEGRNBDCBCBGFDCEGDJCBDQCEGDDEDHCDBDCDBGBNDJCHFPDJCIEDCDFDDGBGHEGBGEHGMBCEDCEIGDDBDGHBGDDDEDGDBDDDGEMDKBICEGQEICBGDBDCIEHINDBHDGBGBDGFCMBNBGBGJCBCENDDBHNECDQBGBDCDDEDCHDEHCFHGMDHDBKEIGDFCBDGEDCDQFGBHGBNBIMIDNIDBICDBGEGDDECDBGBHCDDBHCFICDJCEDCECFDCFHCBQCMDDHHFDCBCIDHEDCHBHQNBDKFDGHDBCEGDDMFDCEHIGBGBCDJDCFCBCFDHMGDEGBQCDBHCFDLHEDGBCJGENBGFCBHDNDECECDEDDHDCDDEICJCHBGDBGHBCJDQDCEGHDBPBDCBNHBDGEMHDIDCFDCHEDHCDHDHBNJCBGIECFCBDKDFDDGDBGBCBKBCDDHDFGHDECSFHDCDBHDHNBGEKBHDCDIBHDCHEDHCDHDBHHCFDNDBGEID
      

  5.   

    不影响的!先将上述字符串转换为Byte数组:Dim tString As String
    Dim tBytes() As BytetString="ABBCBCBCDBDCBC……"
    tBytes()=Strconv(tString, vbFromUnicode)
    然后你让:
    tBytes(i)=tBytes(i)-65 (i=0 to UBound(tBytes()))
    然后你记得根据tBytes(i)去查表就可以了。我那样编码的目的就是为了速度快,只要加减65就可以迅速转换为字母。
      

  6.   

    搞定,333个字节:Private Sub Command1_Click()
    Dim b(332) As Byte, temp As Byte, num As Byte, i As Integer, j As Byte
    b(0) = 255
    b(1) = 239
    b(2) = 119
    b(3) = 63
    b(4) = 219
    b(5) = 237
    b(6) = 158
    b(7) = 252
    b(8) = 234
    b(9) = 39
    b(10) = 143
    b(11) = 121
    b(12) = 117
    b(13) = 211
    b(14) = 11800
    b(15) = 79
    b(16) = 115
    b(17) = 134
    b(18) = 233
    b(19) = 233
    b(20) = 157
    b(21) = 238
    b(22) = 172
    b(23) = 82
    b(24) = 181
    b(25) = 51
    b(26) = 201
    b(27) = 94
    b(28) = 60
    b(29) = 15
    b(30) = 83
    b(31) = 43
    b(32) = 171
    b(33) = 241
    b(34) = 214
    b(35) = 22
    b(36) = 111
    b(37) = 21
    b(38) = 166
    b(39) = 170
    b(40) = 236
    b(41) = 81
    b(42) = 248
    b(43) = 207
    b(44) = 1
    b(45) = 170
    b(46) = 80
    b(47) = 124
    b(48) = 151
    b(49) = 126
    b(50) = 162
    b(51) = 116
    b(52) = 51
    b(53) = 251
    b(54) = 9
    b(55) = 29
    b(56) = 92
    b(57) = 166
    b(58) = 21
    b(59) = 157
    b(60) = 162
    b(61) = 136
    b(62) = 95
    b(63) = 42
    b(64) = 198
    b(65) = 96
    b(66) = 189
    b(67) = 89
    b(68) = 100
    b(69) = 94
    b(70) = 198
    b(71) = 167
    b(72) = 16
    b(73) = 172
    b(74) = 184
    b(75) = 184
    b(76) = 205
    b(77) = 224
    b(78) = 139
    b(79) = 119
    b(80) = 42
    b(81) = 75
    b(82) = 13
    b(83) = 132
    b(84) = 242
    b(85) = 65
    b(86) = 70
    b(87) = 35
    b(88) = 185
    b(89) = 125
    b(90) = 215
    b(91) = 50
    b(92) = 201
    b(93) = 71
    b(94) = 172
    b(95) = 67
    b(96) = 105
    b(97) = 73
    b(98) = 236
    b(99) = 192
    b(100) = 50
    b(101) = 147
    b(102) = 113
    b(103) = 208
    b(104) = 8
    b(105) = 156
    b(106) = 99
    b(107) = 19
    b(108) = 158
    b(109) = 192
    b(110) = 245
    b(111) = 204
    b(112) = 198
    b(113) = 40
    b(114) = 68
    b(115) = 31
    b(116) = 146
    b(117) = 249
    b(118) = 153
    b(119) = 38
    b(120) = 173
    b(121) = 69
    b(122) = 142
    b(123) = 83
    b(124) = 21
    b(125) = 90
    b(126) = 44
    b(127) = 38
    b(128) = 39
    b(129) = 19
    b(130) = 251
    b(131) = 12
    b(132) = 65
    b(133) = 238
    b(134) = 193
    b(135) = 97
    b(136) = 150
    b(137) = 120
    b(138) = 28
    b(139) = 129
    b(140) = 218
    b(141) = 230
    b(142) = 102
    b(143) = 1
    b(144) = 89
    b(145) = 37
    b(146) = 74
    b(147) = 134
    b(148) = 43
    b(149) = 38
    b(150) = 61
    b(151) = 152
    b(152) = 161
    b(153) = 133
    b(154) = 248
    b(155) = 101
    b(156) = 34
    b(157) = 54
    b(158) = 18
    b(159) = 252
    b(160) = 140
    b(161) = 128
    b(162) = 74
    b(163) = 84
    b(164) = 174
    b(165) = 57
    b(166) = 245
    b(167) = 70
    b(168) = 18
    b(169) = 203
    b(170) = 21
    b(171) = 40
    b(172) = 83
    b(173) = 17
    b(174) = 15
    b(175) = 226
    b(176) = 104
    b(177) = 36
    b(178) = 3
    b(179) = 106
    b(180) = 157
    b(181) = 23
    b(182) = 58
    b(183) = 206
    b(184) = 3
    b(185) = 181
    b(186) = 2
    b(187) = 196
    b(188) = 95
    b(189) = 180
    b(190) = 10
    b(191) = 23
    b(192) = 176
    b(193) = 170
    b(194) = 241
    b(195) = 219
    b(196) = 40
    b(197) = 76
    b(198) = 4
    b(199) = 10
    b(200) = 67
    b(201) = 45
    b(202) = 213
    b(203) = 162
    b(204) = 166
    b(205) = 36
    b(206) = 184
    b(207) = 19
    b(208) = 233
    b(209) = 201
    b(210) = 106
    b(211) = 229
    b(212) = 85
    b(213) = 129
    b(214) = 193
    b(215) = 176
    b(216) = 2
    b(217) = 18
    b(218) = 231
    b(219) = 67
    b(220) = 17
    b(221) = 225
    b(222) = 212
    b(223) = 86
    b(224) = 12
    b(225) = 198
    b(226) = 38
    b(227) = 188
    b(228) = 232
    b(229) = 68
    b(230) = 11
    b(231) = 216
    b(232) = 171
    b(233) = 99
    b(234) = 49
    b(235) = 81
    b(236) = 96
    b(237) = 26
    b(238) = 18
    b(239) = 41
    b(240) = 87
    b(241) = 45
    b(242) = 32
    b(243) = 153
    b(244) = 198
    b(245) = 16
    b(246) = 132
    b(247) = 33
    b(248) = 26
    b(249) = 91
    b(250) = 105
    b(251) = 219
    b(252) = 236
    b(253) = 140
    b(254) = 112
    b(255) = 176
    b(256) = 51
    b(257) = 141
    b(258) = 28
    b(259) = 48
    b(260) = 104
    b(261) = 34
    b(262) = 61
    b(263) = 74
    b(264) = 205
    b(265) = 4
    b(266) = 196
    b(267) = 65
    b(268) = 84
    b(269) = 186
    b(270) = 42
    b(271) = 136
    b(272) = 147
    b(273) = 208
    b(274) = 46
    b(275) = 52
    b(276) = 143
    b(277) = 17
    b(278) = 100
    b(279) = 25
    b(280) = 240
    b(281) = 140
    b(282) = 1
    b(283) = 162
    b(284) = 57
    b(285) = 164
    b(286) = 88
    b(287) = 156
    b(288) = 97
    b(289) = 115
    b(290) = 149
    b(291) = 171
    b(292) = 48
    b(293) = 204
    b(294) = 90
    b(295) = 14
    b(296) = 5
    b(297) = 180
    b(298) = 226
    b(299) = 192
    b(300) = 71
    b(301) = 86
    b(302) = 129
    b(303) = 20
    b(304) = 141
    b(305) = 163
    b(306) = 113
    b(307) = 202
    b(308) = 8
    b(309) = 39
    b(310) = 50
    b(311) = 23
    b(312) = 138
    b(313) = 229
    b(314) = 30
    b(315) = 110
    b(316) = 138
    b(317) = 98
    b(318) = 3
    b(319) = 136
    b(320) = 244
    b(321) = 148
    b(322) = 88
    b(323) = 193
    b(324) = 116
    b(325) = 152
    b(326) = 163
    b(327) = 113
    b(328) = 154
    b(329) = 140
    b(330) = 225
    b(331) = 18
    b(332) = 5
    Debug.Print 2; 3; 5;
    For i = 0 To 332
    temp = b(i)
    For j = 0 To 7
    num = temp Mod 2
    If num = 1 Then Debug.Print 30 * i + Array(7, 11, 13, 17, 19, 23, 29, 31)(j);
    temp = temp \ 2
    Next
    Next
    End Sub
      

  7.   

    但假如我把1亿范围内的素数存入一个数组,至少也要3.18 MByte,太大了.递归求质数太慢.
      

  8.   

    小软件发布在 http://download.enet.com.cn/html/033432005111501.html,算法尚待改进,欢迎大家提出宝贵意见.
      

  9.   

    最新有两个重大突破:1、根据频率表分析发现:3以上的改变量都是2的倍数。不需编码,直接可以用4Bit来存储。
    0 1 205 0 202 0 299 0 101 0 119 0 105 0 54 0 33 0 40 0 15 0 16 0 15 0 3 0 5 0 11 0 1 0 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 02、我发现一种新算法,可以在CR433MHz的老电脑上,在0.6s内计算出1000000以内的素数。其计算量仅是1000000的n倍而已(可能是2倍左右),与跳蚤算法有相似的地方。
      

  10.   

    计算素数的新算法,与跳蚤算法一样,属于“摆地摊”算法的一种(以空间换速度)。优点是速度比较快。
    原理是这样的:
    假如有100个犯人,你叫到2号犯人的时候,从第4号开始每2人枪毙一个;叫到3号犯人的时候,从6号犯人开始每3人毙一个;叫到5号犯人的时候,从10号开始每5人毙一个……根据这个规律,毙到最后,还活着的犯人的编号全部都是素数。这个算法我取名叫做“枪毙算法”(如果你觉得这个名字太恐怖,也可以叫做“擦除算法”)。
    这个算法的缺点是:需要内存空间比较大(“摆地摊”式算法的特性)。计算n以内的素数需要n个元素的空间。在32位CPU上,n*4字节最快。如果你牺牲一些速度,用n/8的空间也可以(每个Bit记录一个逻辑值)。
    计算100,000,000以内的素数最快速的存储方式需要400MB的内存空间,在CR433MHz电脑上大约需要60s左右。Private Sub Command1_Click()
      Dim tLongs() As Long
      Dim tLongs_Length As Long
      Dim tLongs_Index As Long
      Dim tString As String
      Dim tOnTimer As Double
      
      tOnTimer = Timer
      tLongs() = PrimeNumbersGet(1000000)
      tString = tString & Abs(Timer - tOnTimer)
      tLongs_Length = UBound(tLongs())
        
      'For tLongs_Index = 0 To tLongs_Length
      '  tString = tString & " " & tLongs(tLongs_Index)
      'Next
      
      Text1.Text = tString
    End SubPublic Function PrimeNumbersGet(ByVal pLimit As Long) As Long()
      Dim tOutLongs() As Long
      Dim tOutLongs_Length As Long
      
      Dim tLineDatas() As Boolean
      ReDim tLineDatas(pLimit)
      
      Dim tLineDatas_Index As Long
      Dim tStep_Index As Long
      Dim tStep_Start As Long
      
      tOutLongs_Length = 1
      
      For tLineDatas_Index = 2 To pLimit
        If Not tLineDatas(tLineDatas_Index) Then
          tStep_Start = tLineDatas_Index * 2
          For tStep_Index = tStep_Start To pLimit Step tLineDatas_Index
            tLineDatas(tStep_Index) = True
          Next
          tOutLongs_Length = tOutLongs_Length + 1
        End If
      Next
      
      ReDim Preserve tOutLongs(tOutLongs_Length)
      
      For tLineDatas_Index = 1 To pLimit
        If Not tLineDatas(tLineDatas_Index) Then
          tOutLongs(tLongs_Index) = tLineDatas_Index
          tLongs_Index = tLongs_Index + 1
        End If
      Next
      
      PrimeNumbersGet = tOutLongs()
    End Function
      

  11.   

    In mathematics, the Sieve of Atkin is a fast, modern algorithm for finding all prime numbers up to a specified integer. It is an optimized version of the ancient Sieve of Eratosthenes. It was created by A. O. L. Atkin and Daniel J. Bernstein1.Algorithm:
    First, write four things :A list to hold primes you've found. It starts out filled with the prime numbers below sixty (60=5!). Three lists to hold numbers that remain to be checked and have certain remainders when divided by sixty. They start out filled with all integers from sixty up to the last number you care about, as long as they fit the remainders in one of these classes (notice that numbers with some remainders are ignored completely) : 
    Remainders of 1, 13, 17, 29, 37, 41, 49, or 53. 
    Remainders of 7, 19, 31, or 43. 
    Remainders of 11, 23, 47, or 59. Then, for each remaining number, n :In class one, count the number of combinations of x > 0 and y > 0 that are solutions to 4x^2 + y^2 = n. 
    In class two, count the number of combinations of x > 0 and y > 0 that are solutions to 3x^2 + y^2 = n. 
    In class three, count the number of combinations of x > 0, y > 0, and x>y that are solutions to 3x^2 - y^2 = n. 
    If the count was even, delete the number from the list.Then perform the Sieve of Eratosthenes with a small modification :
    For each prime seven or higher (2,3 and 5 are already excluded in the first place), delete all multiples of the prime squared.Finally, move the remaining numbers to the list of primes.Reference:A.O.L. Atkin, D.J. Bernstein, Prime sieves using binary quadratic forms, Math. Comp. 73 (2004), 1023-1030. <http://citeseer.ist.psu.edu/atkin99prime.html>Source code in C:
    http://cr.yp.to/primegen.html
      

  12.   

    哇!蒋委员长也来了。委员长满噻!满噻!满满噻!最新成果:将时间从0.6s降低到0.5-0.43s左右。
    1,000,000 0.5s-0.43s
    10,000,000 4.8sPublic Function PrimeNumbersGet(ByVal pLimit As Long) As Long()
      Dim tOutLongs() As Long
      Dim tOutLongs_Index As Long
      
      Dim tLineDatas() As Boolean
      Dim tLineDatas_Index As Long
      Dim tLineDatas_Index_Over As Long
      
      ReDim tLineDatas(pLimit)
      
      Dim tStep_Index As Long
      Dim tStep_Start As Long
        
      tLineDatas_Index_Over = Sqr(pLimit) + 1
      
      For tLineDatas_Index = 2 To tLineDatas_Index_Over
        If Not tLineDatas(tLineDatas_Index) Then
          tStep_Start = tLineDatas_Index + tLineDatas_Index
          For tStep_Index = tStep_Start To pLimit Step tLineDatas_Index
            tLineDatas(tStep_Index) = True
          Next
        End If
      Next
        
      tOutLongs_Index = 2  ReDim Preserve tOutLongs(tOutLongs_Index)
      tOutLongs(0) = 1: tOutLongs(1) = 2  For tLineDatas_Index = 3 To pLimit Step 2
        If Not tLineDatas(tLineDatas_Index) Then
          ReDim Preserve tOutLongs(tOutLongs_Index)
          tOutLongs(tOutLongs_Index) = tLineDatas_Index
          tOutLongs_Index = tOutLongs_Index + 1
        End If
      Next
      
      PrimeNumbersGet = tOutLongs()
    End Function
      

  13.   

    另外呢,想知道某个数是不是素数和想求某个范围内的素数是不同的。
    想判断一个数N是不是素数,只要将N与SQR(N)以内的素数依次取余就可以了。
    根据这个道理,求2147483647(long类型的极限)以内的的任何一个数字是不是素数,只要有46341以内的素数表就够了。46341以内的素数有4792个。
      

  14.   

    非常感谢 KiteGirl(小仙妹) 和 jiangsheng(蒋晟.MSMVP2004Jan) 给予的启发,特此将改进后的代码与大家共享。
    http://blog.csdn.net/northwolves/archive/2005/11/18/532695.aspx代码执行结果(编译前):n=100,prime count=25, The calulating time is 0.0000 seconds.
    n=1000,prime count=168, The calulating time is 0.0000 seconds.
    n=10000,prime count=1229, The calulating time is 0.0000 seconds.
    n=100000,prime count=9592, The calulating time is 0.0002 seconds.
    n=1000000,prime count=78498, The calulating time is 0.2269 seconds.
    n=10000000,prime count=664579, The calulating time is 2.0719 seconds.
    n=100000000,prime count=5761455, The calulating time is 15.2344 seconds.
      

  15.   

    To  jiangsheng(蒋晟.MSMVP2004Jan)Thanks a lot for your article above.I have read it in about 1997,but I can't search it from GOOGLE now.
    I remember that someone have said the following conditions is "n is prime if and only if":In class one, count the number of combinations of x > 0 and y > 0 that are solutions to 4x^2 + y^2 = n. 
    In class two, count the number of combinations of x > 0 and y > 0 that are solutions to 3x^2 + y^2 = n. 
    In class three, count the number of combinations of x > 0, y > 0, and x>y that are solutions to 3x^2 - y^2 = n. 
    If the count was even, delete the number from the list.I think that the last class is hard to use for enuming the prime numbers.Any idea?
      

  16.   

    测试代码:寻找2147483646以下最大的素数(long类型的最大极限2147483647恰好是一个素数,因此从2147483646开始向下)。Private Sub Command7_Click()
      Dim tPrimes() As Long
      Dim tPrimes_Index As Long
      Dim tIndex As Long
      Dim tTemp As Boolean
      tTemp = NumberIsPrime(10)
      
      OnTimer = Timer  For tIndex = 2147483646 To 10 Step -1
        If NumberIsPrime(tIndex) Then
          Text1.Text = tIndex
          Exit For
        End If
      Next
      Text1.Text = Text1.Text & " " & Timer - OnTimer
      
    End Sub
    下面是modUltraPrimeNumbers.bas模块
    'NumberIsPrime判断long类型范围内任意一个大于3的正整数是否是素数(该函数测量数值与内存容量无关)。
    'PrimeNumbersGet获得pLimit范围内的素数表(“枪毙法”,需要占用pLimit*4的内存空间。)。Option ExplicitPrivate NumberIsPrime_PBT() As Long
    Private NumberIsPrime_PBT_Enabled As Boolean
    Private NumberIsPrime_PBT_Length As LongPublic Function NumberIsPrime(ByVal pNumber As Long) As Boolean
      Dim tOutBool As Boolean
      
      If Not NumberIsPrime_PBT_Enabled Then
        NumberIsPrime_PBT() = PrimeNumbersGet(65536)
        NumberIsPrime_PBT_Length = UBound(NumberIsPrime_PBT())
        NumberIsPrime_PBT_Enabled = True
      End If
      
      Dim tPBT_Index As Long
      Dim tPBT_Limit As Long
      
      tPBT_Limit = Sqr(pNumber) + 1
      
      Dim tCheck_LoopEnd As Boolean
      
      For tPBT_Index = 1 To NumberIsPrime_PBT_Length
        tOutBool = CBool(pNumber Mod NumberIsPrime_PBT(tPBT_Index))
        tCheck_LoopEnd = NumberIsPrime_PBT(tPBT_Index) > tPBT_Limit
        tCheck_LoopEnd = tCheck_LoopEnd Or (Not tOutBool)
        If tCheck_LoopEnd Then Exit For
      Next
      
      NumberIsPrime = tOutBool
    End FunctionPublic Function PrimeNumbersGet(ByVal pLimit As Long) As Long()
      Dim tOutLongs() As Long
      Dim tOutLongs_Index As Long
      
      Dim tLineDatas() As Boolean
      Dim tLineDatas_Index As Long
      Dim tLineDatas_Index_Over As Long
      
      ReDim tLineDatas(pLimit)
      
      Dim tStep_Index As Long
      Dim tStep_Start As Long
        
      tLineDatas_Index_Over = Sqr(pLimit) + 1
      
      For tLineDatas_Index = 2 To tLineDatas_Index_Over
        If Not tLineDatas(tLineDatas_Index) Then
          tStep_Start = tLineDatas_Index + tLineDatas_Index
          For tStep_Index = tStep_Start To pLimit Step tLineDatas_Index
            tLineDatas(tStep_Index) = True
          Next
        End If
      Next
        
      tOutLongs_Index = 2  ReDim Preserve tOutLongs(tOutLongs_Index)
      tOutLongs(0) = 1: tOutLongs(1) = 2  For tLineDatas_Index = 3 To pLimit Step 2
        If Not tLineDatas(tLineDatas_Index) Then
          ReDim Preserve tOutLongs(tOutLongs_Index)
          tOutLongs(tOutLongs_Index) = tLineDatas_Index
          tOutLongs_Index = tOutLongs_Index + 1
        End If
      Next
      
      PrimeNumbersGet = tOutLongs()
    End Function
      

  17.   

    呵呵!下面这个代码离快速分解质因数只有一步之遥了……Private Sub Command8_Click()
      Dim tLongs() As Long
      Dim tIndex As Long
      Dim tIndex2 As Long
      For tIndex = 0 To 100
        Text1.Text = Text1.Text & tIndex & "["
        tLongs() = NumberAnalyzePrimes(tIndex)
        For tIndex2 = 0 To UBound(tLongs())
          Text1.Text = Text1.Text & " " & tLongs(tIndex2)
        Next
        Text1.Text = Text1.Text & " ];" & vbCrLf
      Next
    End SubNumberAnalyzePrimes函数可以求出long类型范围内任意正整数由哪些素数相乘,但还不是分解质因数。根据它得出的结果分解质因数已经是相当简单的事情了(简单到我都懒得写了)。比如100可以分解成2,5两种素数,但每一种素数的具体数量还需要另外的过程去求一下。NumberIsPrime函数可以判断long类型范围内任意正整数是否是素数。它的特点是可以直接求99000000-100000000之间的素数,而不需要把99000000之前的算出来。NumberIsPrime函数求素数不如PrimeNumbersGet快,99000000-100000000之间素数表需要13秒时间。但是这个方法不占用大量的内存空间。NumberAnalyzePrimes函数和NumberIsPrime函数的代价是需要一个26K字节左右内存空间存储65536以内的素数表。在第一次使用这两个函数其中任意一个的时候,以PrimeNumbersGet创建这个基本素数表。之后再使用这些函数,将直接从素数表里读取而不是计算这些基本素数。这里用到一个规律就是:求n以内的任意一个数是否素数,或是分解质因数,只要Sqr(n)以内的素数表就够了。因此,求2^32以内的任意一个整数是否素数只要2^16以内的素数表就可以了,这就是为什么基本素数表是65536以内素数的原因。
    modUltraPrimeNumbers.bas模块Option ExplicitPrivate priPrimesTable() As Long            '
    Private priPrimesTable_Enabled As Boolean   '
    Private priPrimesTable_Length As Long       'Public Function NumberAnalyzePrimes(ByVal pNumber As Long) As Long()
      '
      Dim tOutLongs() As Long
      Dim tOutLongs_Index As Long
      
      PrimesTableCreate  Dim tPrimesTable_Index As Long
      Dim tPrimesTable_Limit As Long
      
      tPrimesTable_Limit = Sqr(pNumber) + 1
      
      Dim tCheck_LoopEnd As Boolean
      Dim tCheck_FindPrime As Boolean
      
      For tPrimesTable_Index = 1 To priPrimesTable_Length
        
        tCheck_FindPrime = Not CBool(pNumber Mod priPrimesTable(tPrimesTable_Index))
        tCheck_LoopEnd = priPrimesTable(tPrimesTable_Index) > tPrimesTable_Limit
        
        If tCheck_FindPrime Then
          
          ReDim Preserve tOutLongs(tOutLongs_Index)
          tOutLongs(tOutLongs_Index) = priPrimesTable(tPrimesTable_Index)
          tOutLongs_Index = tOutLongs_Index + 1
        
        End If
        If tCheck_LoopEnd Then Exit For
      Next
      
      If Not CBool(tOutLongs_Index) Then
          ReDim tOutLongs(tOutLongs_Index)
          tOutLongs(tOutLongs_Index) = pNumber
      End If
      
      NumberAnalyzePrimes = tOutLongs()
    End FunctionPublic Function NumberIsPrime(ByVal pNumber As Long) As Boolean
      Dim tOutBool As Boolean
      
      PrimesTableCreate  Dim tPrimesTable_Index As Long
      Dim tPrimesTable_Limit As Long
      
      tPrimesTable_Limit = Sqr(pNumber) + 1
      
      Dim tCheck_LoopEnd As Boolean
      
      For tPrimesTable_Index = 1 To priPrimesTable_Length
        tOutBool = CBool(pNumber Mod priPrimesTable(tPrimesTable_Index))
        tCheck_LoopEnd = priPrimesTable(tPrimesTable_Index) > tPrimesTable_Limit
        tCheck_LoopEnd = tCheck_LoopEnd Or (Not tOutBool)
        If tCheck_LoopEnd Then Exit For
      Next
      
      NumberIsPrime = tOutBool
    End FunctionPublic Function PrimeNumbersGet(ByVal pLimit As Long) As Long()
      Dim tOutLongs() As Long
      Dim tOutLongs_Index As Long
      
      Dim tLineDatas() As Boolean
      Dim tLineDatas_Index As Long
      Dim tLineDatas_Index_Over As Long
      
      ReDim tLineDatas(pLimit)
      
      Dim tStep_Index As Long
      Dim tStep_Start As Long
        
      tLineDatas_Index_Over = Sqr(pLimit) + 1
      
      For tLineDatas_Index = 2 To tLineDatas_Index_Over
        If Not tLineDatas(tLineDatas_Index) Then
          tStep_Start = tLineDatas_Index + tLineDatas_Index
          For tStep_Index = tStep_Start To pLimit Step tLineDatas_Index
            tLineDatas(tStep_Index) = True
          Next
        End If
      Next
        
      tOutLongs_Index = 2  ReDim Preserve tOutLongs(tOutLongs_Index)
      tOutLongs(0) = 1: tOutLongs(1) = 2  For tLineDatas_Index = 3 To pLimit Step 2
        If Not tLineDatas(tLineDatas_Index) Then
          ReDim Preserve tOutLongs(tOutLongs_Index)
          tOutLongs(tOutLongs_Index) = tLineDatas_Index
          tOutLongs_Index = tOutLongs_Index + 1
        End If
      Next
      
      PrimeNumbersGet = tOutLongs()
    End FunctionPrivate Sub PrimesTableCreate()
      If Not priPrimesTable_Enabled Then
        priPrimesTable() = PrimeNumbersGet(65536)
        priPrimesTable_Length = UBound(priPrimesTable())
        priPrimesTable_Enabled = True
      End If
    End Sub
      

  18.   

    下面这个测试代码更能说明问题:
    分解2147483546 - 2147483646的质因数素数成分。
    Private Sub Command8_Click()
      Dim tLongs() As Long
      Dim tIndex As Long
      Dim tIndex2 As Long
      For tIndex = 2147483546 To 2147483646
        Text1.Text = Text1.Text & tIndex & "["
        tLongs() = NumberAnalyzePrimes(tIndex)
        For tIndex2 = 0 To UBound(tLongs())
          Text1.Text = Text1.Text & " " & tLongs(tIndex2)
        Next
        Text1.Text = Text1.Text & " ];" & vbCrLf
      Next
    End Sub结果是:
    2147483613[ 3 11 2953 22037 ];
    2147483614[ 2 101 ];
    2147483615[ 5 31 ];
    2147483616[ 2 3 2731 8191 ];
    2147483617[ 6733 ];
    2147483618[ 2 7 367 ];
    2147483619[ 3 23 353 29389 ];
    2147483620[ 2 5 4603 23327 ];
    2147483621[ 14741 ];
    2147483622[ 2 3 17 467 45083 ];
    2147483623[ 79 967 28111 ];
    2147483624[ 2 11 13 ];
    2147483625[ 3 5 7 199 4111 ];
    2147483626[ 2 19 37 ];
    2147483627[ 47 53 ];
    2147483628[ 2 3 ];
    2147483629[ 2147483629 ]; '它是一个素数
    2147483630[ 2 5 6553 32771 ];
    2147483631[ 3 137 263 19867 ];
    2147483632[ 2 7 73 ];
    2147483633[ 5843 ];
    2147483634[ 2 3 12097 29587 ];
    2147483635[ 5 11 337 ];
    2147483636[ 2 ];
    2147483637[ 3 13 ];
    2147483638[ 2 2969 ];
    2147483639[ 7 17 ];
    2147483640[ 2 3 5 29 43 113 127 ];
    2147483641[ 2699 ];
    2147483642[ 2 23 ];
    2147483643[ 3 ];
    2147483644[ 2 233 1103 2089 ];
    2147483645[ 5 19 ];
    2147483646[ 2 3 7 11 31 151 331 ];在递增的For循环里,算到2147483646至少在long类型里就是到头了,否则要溢出。long的最大极限2147483647本身是个素数。如果你有100000000范围内的素数表,你可以分解10000000000000000以内的质因数。
      

  19.   

    快速分解质因数函数的最终结果:测试代码:Private Sub Command9_Click()
      Dim tLongs() As Long
      Dim tIndex As Long
      Dim tIndex2 As Long
      Dim tString As String
      For tIndex = 2147483546 To 2147483646
      'For tIndex = 2 To 100
        tString = tString & tIndex & "["
        tLongs() = NumberPrimeFactors(tIndex)
        For tIndex2 = 0 To UBound(tLongs())
          tString = tString & " " & tLongs(tIndex2)
        Next
        tString = tString & " ];" & vbCrLf
      Next
      Text1.Text = tString
    End Sub计算结果:[上略]
    2147483636[ 2 2 536870909 ];
    2147483637[ 3 3 3 13 6118187 ];
    2147483638[ 2 2969 361651 ];
    2147483639[ 7 17 18046081 ];
    2147483640[ 2 2 2 3 5 29 43 113 127 ];
    2147483641[ 2699 795659 ];
    2147483642[ 2 23 46684427 ];
    2147483643[ 3 715827881 ];
    2147483644[ 2 2 233 1103 2089 ];
    2147483645[ 5 19 22605091 ];
    2147483646[ 2 3 3 7 11 31 151 331 ];modUltraPrimeNumbers.bas模块内容Option ExplicitPrivate priPrimesTable() As Long            '
    Private priPrimesTable_Enabled As Boolean   '
    Private priPrimesTable_Length As Long       'Public Function NumberPrimeFactors(ByVal pNumber As Long) As Long()
      Dim tOutLongs() As Long
      Dim tOutLongs_Length As Long
      
      Dim tAnalyzePrimes() As Long
      Dim tAnalyzePrimes_Length As Long
      Dim tAnalyzePrimes_Index As Long
                
      Dim tNumber As Long
      
      tNumber = pNumber
              
      tAnalyzePrimes() = NumberAnalyzePrimes(tNumber)
      tAnalyzePrimes_Length = UBound(tAnalyzePrimes())
      
      For tAnalyzePrimes_Index = 0 To tAnalyzePrimes_Length
        If tNumber = tAnalyzePrimes(tAnalyzePrimes_Index) Then
          ReDim Preserve tOutLongs(tOutLongs_Length)
          tOutLongs(tOutLongs_Length) = tAnalyzePrimes(tAnalyzePrimes_Index)
          tOutLongs_Length = tOutLongs_Length + 1
          Exit For
        End If
        Do
          If Not CBool(tNumber Mod tAnalyzePrimes(tAnalyzePrimes_Index)) Then
              ReDim Preserve tOutLongs(tOutLongs_Length)
              tOutLongs(tOutLongs_Length) = tAnalyzePrimes(tAnalyzePrimes_Index)
              tNumber = tNumber \ tAnalyzePrimes(tAnalyzePrimes_Index)
              tOutLongs_Length = tOutLongs_Length + 1
            Else
              If tNumber > 1 And tAnalyzePrimes_Index = tAnalyzePrimes_Length Then
                ReDim Preserve tOutLongs(tOutLongs_Length)
                tOutLongs(tOutLongs_Length) = tNumber
              End If
              Exit Do
          End If
        Loop
      Next
      NumberPrimeFactors = tOutLongs()
    End FunctionPublic Function NumberAnalyzePrimes(ByVal pNumber As Long) As Long()  Dim tOutLongs() As Long
      Dim tOutLongs_Index As Long
      
      PrimesTableCreate  Dim tPrimesTable_Index As Long
      Dim tPrimesTable_Limit As Long
      
      tPrimesTable_Limit = Sqr(pNumber) + 1
      
      Dim tCheck_LoopEnd As Boolean
      Dim tCheck_FindPrime As Boolean
      
      For tPrimesTable_Index = 1 To priPrimesTable_Length
        
        tCheck_FindPrime = Not CBool(pNumber Mod priPrimesTable(tPrimesTable_Index))
        tCheck_LoopEnd = priPrimesTable(tPrimesTable_Index) > tPrimesTable_Limit
        
        If tCheck_FindPrime Then
          
          ReDim Preserve tOutLongs(tOutLongs_Index)
          tOutLongs(tOutLongs_Index) = priPrimesTable(tPrimesTable_Index)
          tOutLongs_Index = tOutLongs_Index + 1
        
        End If
        
        If tCheck_LoopEnd Then Exit For
      Next
      
      If Not CBool(tOutLongs_Index) Then
          ReDim tOutLongs(tOutLongs_Index)
          tOutLongs(tOutLongs_Index) = pNumber
      End If
      
      NumberAnalyzePrimes = tOutLongs()
    End FunctionPublic Function NumberIsPrime(ByVal pNumber As Long) As Boolean
      Dim tOutBool As Boolean
      
      PrimesTableCreate  Dim tPrimesTable_Index As Long
      Dim tPrimesTable_Limit As Long
      
      tPrimesTable_Limit = Sqr(pNumber) + 1
      
      Dim tCheck_LoopEnd As Boolean
      
      For tPrimesTable_Index = 1 To priPrimesTable_Length
        tOutBool = CBool(pNumber Mod priPrimesTable(tPrimesTable_Index))
        tCheck_LoopEnd = priPrimesTable(tPrimesTable_Index) > tPrimesTable_Limit
        tCheck_LoopEnd = tCheck_LoopEnd Or (Not tOutBool)
        If tCheck_LoopEnd Then Exit For
      Next
      
      NumberIsPrime = tOutBool
    End FunctionPublic Function PrimeNumbersGet(ByVal pLimit As Long) As Long()
      Dim tOutLongs() As Long
      Dim tOutLongs_Index As Long
      
      Dim tLineDatas() As Boolean
      Dim tLineDatas_Index As Long
      Dim tLineDatas_Index_Over As Long
      
      ReDim tLineDatas(pLimit)
      
      Dim tStep_Index As Long
      Dim tStep_Start As Long
        
      tLineDatas_Index_Over = Sqr(pLimit) + 1
      
      For tLineDatas_Index = 2 To tLineDatas_Index_Over
        If Not tLineDatas(tLineDatas_Index) Then
          tStep_Start = tLineDatas_Index + tLineDatas_Index
          For tStep_Index = tStep_Start To pLimit Step tLineDatas_Index
            tLineDatas(tStep_Index) = True
          Next
        End If
      Next
        
      tOutLongs_Index = 2  ReDim Preserve tOutLongs(tOutLongs_Index)
      tOutLongs(0) = 1: tOutLongs(1) = 2  For tLineDatas_Index = 3 To pLimit Step 2
        If Not tLineDatas(tLineDatas_Index) Then
          ReDim Preserve tOutLongs(tOutLongs_Index)
          tOutLongs(tOutLongs_Index) = tLineDatas_Index
          tOutLongs_Index = tOutLongs_Index + 1
        End If
      Next
      
      PrimeNumbersGet = tOutLongs()
    End FunctionPrivate Sub PrimesTableCreate()
      If Not priPrimesTable_Enabled Then
        priPrimesTable() = PrimeNumbersGet(65536)
        priPrimesTable_Length = UBound(priPrimesTable())
        priPrimesTable_Enabled = True
      End If
    End Sub
      

  20.   

    you should download the source code in C and take a look.
      

  21.   

    I have found some code written by C which can list prime numbers from 0 to 10^8 within 1.2 seconds. 
    Though I have accelerated my codes nearly ten times(
    http://blog.csdn.net/northwolves/archive/2005/11/19/533038.aspx),the best way to solve this by VB is to compile it to dll then cite it in VB perhaps. No language can exceed it.
      

  22.   

    最新进展:更新代码。
    以tpPrimeFactor类型表示素数及级数,将n个p表示为p^n。减少了记录结果的数据空间。2147483637=[ 3^3 13^1 6118187^1 ];
    2147483638=[ 2^1 2969^1 361651^1 ];
    2147483639=[ 7^1 17^1 18046081^1 ];
    2147483640=[ 2^3 3^1 5^1 29^1 43^1 113^1 127^1 ];
    2147483641=[ 2699^1 795659^1 ];
    2147483642=[ 2^1 23^1 46684427^1 ];
    2147483643=[ 3^1 715827881^1 ];
    2147483644=[ 2^2 233^1 1103^1 2089^1 ];
    2147483645=[ 5^1 19^1 22605091^1 ];
    2147483646=[ 2^1 3^2 7^1 11^1 31^1 151^1 331^1 ];增加数据类型tpPrimeFactorPublic Type tpPrimeFactor
      pfPrime As Long                           '素数
      pfCount As Byte                           '级数
    End Type改进后的NumberPrimeFactors函数:Public Function NumberPrimeFactors(ByVal pNumber As Long) As tpPrimeFactor()
      Dim tOutDatas() As tpPrimeFactor
      Dim tOutDatas_Length As Long
      
      Dim tAnalyzePrimes() As Long
      Dim tAnalyzePrimes_Length As Long
      Dim tAnalyzePrimes_Index As Long
                
      Dim tNumber As Long
      
      tNumber = pNumber
              
      tAnalyzePrimes() = NumberAnalyzePrimes(tNumber)
      tAnalyzePrimes_Length = UBound(tAnalyzePrimes())
      
      tOutDatas_Length = tAnalyzePrimes_Length
      
      ReDim tOutDatas(tOutDatas_Length)
      
      For tAnalyzePrimes_Index = 0 To tAnalyzePrimes_Length
        
        With tOutDatas(tAnalyzePrimes_Index)
          .pfPrime = tAnalyzePrimes(tAnalyzePrimes_Index)
        End With
        
        If tNumber = tAnalyzePrimes(tAnalyzePrimes_Index) Then
                
          With tOutDatas(tAnalyzePrimes_Index)
            
            .pfCount = .pfCount + 1
            
          End With
          
          Exit For
        
        End If
        
        Do
          
          If Not CBool(tNumber Mod tAnalyzePrimes(tAnalyzePrimes_Index)) Then
                        
              With tOutDatas(tAnalyzePrimes_Index)
            
                .pfCount = .pfCount + 1
            
              End With
              
              tNumber = tNumber \ tAnalyzePrimes(tAnalyzePrimes_Index)
            
            Else
              
              If tNumber > 1 And tAnalyzePrimes_Index = tAnalyzePrimes_Length Then
                
                tOutDatas_Length = tOutDatas_Length + 1
                
                ReDim Preserve tOutDatas(tOutDatas_Length)
                
                With tOutDatas(tOutDatas_Length)
            
                  .pfPrime = tNumber
                  .pfCount = .pfCount + 1
            
                End With
              
              End If
              
              Exit Do
          
          End If
        
        Loop
      
      Next
      
      NumberPrimeFactors = tOutDatas()
    End Function
      

  23.   


     15  25 
     21  35  49 
     27  45  63  81 
     33  55  77  99  121 
     39  65  91  117  143  169 
     45  75  105  135  165  195  225 
     51  85  119  153  187  221  255  289 
     57  95  133  171  209  247  285  323  361 
     63  105  147  189  231  273  315  357  399  441 
     69  115  161  207  253  299  345  391  437  483  529 
     75  125  175  225  275  325  375  425  475  525  575  625 
     81  135  189  243  297  351  405  459  513  567  621  675  729 
     87  145  203  261  319  377  435  493  551  609  667  725  783  841 
     93  155  217  279  341  403  465  527  589  651  713  775  837  899  961 
     99  165  231  297  363  429  495  561  627  693  759  825  891  957  1023  1089 今天意外发现的一个有趣的情况,上面的奇数(33*33以内)三角形全是合数,不在上述范围内的奇数全是素数,无一例外.可惜对于素数的筛选并没什么有利条件.三角是这样生成的:Sub even(ByVal n As Long)
    Dim i As Long, j As Long
    For i = 1 To n
    For j = 1 To i
    Debug.Print (2 * i + 1) * (2 * j + 1);
    Next
    Debug.Print
    Next
    End SubPrivate Sub Command1_Click()
    even 20
    End Sub
      

  24.   

    用 Long 型数组存放,使用又快又方便,而且楼主给出的数据为 1229个×4字节=4916字节=4.8K
      

  25.   

    我曾经也为写这类程序而痴迷,随着计算的深入,不得不去看数论方面的书。等书看得差不多的时候就会发现这样的程序并没有太大意义。在10^8范围内你可能觉得计算速度已经够快,但是现在RSA密钥的计算是2^1024数量级。如果在筛法之上没有更大的突破大数分解的意义不是很大。
      

  26.   

    用 Long 型数组存放,使用又快又方便,而且楼主给出的数据为 1229个×4字节=4916字节=4.8K
    -----------------我是想存到硬盘上
      

  27.   

    To 口香糖:我曾经也为写这类程序而痴迷,随着计算的深入,不得不去看数论方面的书。等书看得差不多的时候就会发现这样的程序并没有太大意义。在10^8范围内你可能觉得计算速度已经够快,但是现在RSA密钥的计算是2^1024数量级。如果在筛法之上没有更大的突破大数分解的意义不是很大。
    -------------------------------
    赞同你的观点。针对大数,必须用其他手段,如福利液变换,费马定理还有俺们的剩余定理等,太高深了。
    大家有兴趣可以看看:http://primes.utm.edu
    http://www.science-chat.org/group-4346-1001.html