---
title: "快速分解质因数"
author: "cjc"
date: "2005-11-09T00:47:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2005-11-09T00:47:00Z"
id: 299152
url: "https://cjc.iteye.com/blog/299152"
---

笔者在对素数的规律研究中,发现Sieve of Eratosthenes 方法对素数的筛选还是比较快的(参见拙作href="[http://blog.csdn.net/northwolves/archive/2005/04/18/351998.aspx"&gt;http://blog.csdn.net/northwolves/archive/2005/04/18/351998.aspx&lt;/a](http://blog.csdn.net/northwolves/archive/2005/04/18/351998.aspx%22&gt;http://blog.csdn.net/northwolves/archive/2005/04/18/351998.aspx%3C/a)&gt;),将其应用于分解质因数,总体感觉还可以,代码如下:

'\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
'  Target:to get all prime Factors of continuous numbers      

'   Author：northwolves                                                                            

'   Reference："Sieve of Eratosthenes"                                                    

 '  Createdate：2005-11-08 00:00:00                                                   

'\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**

Function GetPrimeFactor(ByVal mynumber As Long, Optional counts As Long = 0) As String 'crash  counts continuous number start by mynumber to prime factors

Dim max As Long, temp As Long, i As Long, p() As Long, scount As Long, s() As String, result() As String  
scount = 0  
max = Int(Sqr(mynumber + counts))

temp = 1  
ReDim p(1 To temp) '  
p(1) = 2

For i = 3 To max Step 2 ' skip those even number  
     sqrtmax = Int(Sqr(i)) + 1 'get the largest non-self factor  of i  
     beprime = True ' init  
  For j = 1 To temp ' try all primer numbers we have found before.  
      If i Mod p(j) = 0 Then beprime = False: Exit For 'not a prime number  
      If p(j) &gt; sqrtmax Then Exit For 'unless we'll waste our time  
  Next

  If beprime = True Then ' then it is a prime number  
      temp = temp + 1 'add to it's count  
      ReDim Preserve p(1 To temp) 'define again  
      p(temp) = i  
    End If  
Next

Dim number As Long  
ReDim result(counts)  
For temp = 0 To counts  
number = mynumber + temp  
result(temp) = number  
scount = 0  
For i = 1 To UBound(p)

again:  
If number Mod p(i) = 0 Then  
scount = scount + 1  
ReDim Preserve s(1 To scount)  
s(scount) = p(i)  
number = number / p(i)  
GoTo again  
End If  
If number = 1 Then Exit For  
Next

If number &gt; 1 Then  
scount = scount + 1  
ReDim Preserve s(1 To scount)  
s(scount) = number  
End If

If scount = 1 Then  
result(temp) = result(temp) & " is a prime number"  
Else  
result(temp) = result(temp) & "=" & Join(s, "\*")  
End If

Next  
GetPrimeFactor = Join(result, vbCrLf)  
Erase p  
Erase s  
Erase result  
End Function

Private Sub Command1\_Click()  
Debug.Print GetPrimeFactor(1234567800, 20)  
End Sub

 

 

\--------------------------------------------------------------------------------------------------

Return:

\---------------------------------------------------------------------------------------------------

1234567800=2\*2\*2\*3\*3\*5\*5\*47\*14593  
1234567801 is a prime number  
1234567802=2\*13\*59\*804803  
1234567803=3\*7\*23\*2556041  
1234567804=2\*2\*1123\*274837  
1234567805=5\*1249\*197689  
1234567806=2\*3\*293\*702257  
1234567807=11\*19\*5907023  
1234567808=2\*2\*2\*2\*2\*2\*2\*31\*241\*1291  
1234567809=3\*3\*71\*877\*2203  
1234567810=2\*5\*7\*41\*149\*2887  
1234567811 is a prime number  
1234567812=2\*2\*3\*17\*6051803  
1234567813=83\*14874311  
1234567814=2\*617283907  
1234567815=3\*5\*13\*13\*367\*1327  
1234567816=2\*2\*2\*29\*29\*183497  
1234567817=7\*2081\*84751  
1234567818=2\*3\*3\*3\*3\*3\*11\*230933  
1234567819=107\*11538017  
1234567820=2\*2\*5\*61728391  
1234567821=3\*411522607  
1234567822=2\*337\*1831703  
1234567823=4241\*291103  
1234567824=2\*2\*2\*2\*3\*7\*73\*50333  
1234567825=5\*5\*49382713  
1234567826=2\*19\*23\*37\*38177  
1234567827=3\*3\*113\*1213931  
1234567828=2\*2\*13\*137\*173297  
1234567829=11\*17\*17\*388351  
1234567830=2\*3\*5\*2797\*14713  
1234567831=7\*176366833  
1234567832=2\*2\*2\*1153\*133843  
1234567833=3\*179\*757\*3037  
1234567834=2\*587\*1051591  
1234567835=5\*569\*433943  
1234567836=2\*2\*3\*3\*1993\*17207  
1234567837=61\*139\*145603  
1234567838=2\*7\*7\*1187\*10613  
1234567839=3\*31\*79\*168037  
1234567840=2\*2\*2\*2\*2\*5\*11\*11\*43\*1483  
1234567841=13\*94966757  
1234567842=2\*3\*131\*1570697  
1234567843=19813\*62311  
1234567844=2\*2\*101\*1277\*2393  
1234567845=3\*3\*3\*5\*7\*19\*29\*2371  
1234567846=2\*17\*151\*191\*1259  
1234567847=47\*251\*104651  
1234567848=2\*2\*2\*3\*51440327  
1234567849=23\*53\*1012771  
1234567850=2\*5\*5\*24691357  
1234567851=3\*11\*41\*912467  
1234567852=2\*2\*7\*521\*84629  
1234567853=1039\*1188227  
1234567854=2\*3\*3\*13\*229\*23039  
1234567855=5\*6101\*40471  
1234567856=2\*2\*2\*2\*77160491  
1234567857=3\*1031\*399149  
1234567858=2\*617283929  
1234567859=7\*1087\*162251  
1234567860=2\*2\*3\*5\*20576131  
1234567861=59\*89\*235111  
1234567862=2\*11\*67\*257\*3259  
1234567863=3\*3\*17\*37\*218083  
1234567864=2\*2\*2\*19\*8122157  
1234567865=5\*379\*651487  
1234567866=2\*3\*7\*1459\*20147  
1234567867=13\*94966759  
1234567868=2\*2\*163\*433\*4373  
1234567869=3\*7499\*54877  
1234567870=2\*5\*31\*31\*128467  
1234567871=271\*4555601  
1234567872=2\*2\*2\*2\*2\*2\*3\*3\*3\*23\*31063  
1234567873=7\*11\*16033349  
1234567874=2\*29\*167\*197\*647  
1234567875=3\*5\*5\*5\*227\*14503  
1234567876=2\*2\*8689\*35521  
1234567877=181\*6820817  
1234567878=2\*3\*205761313  
1234567879=20399\*60521  
1234567880=2\*2\*2\*5\*7\*13\*17\*71\*281  
1234567881=3\*3\*137174209  
1234567882=2\*617283941  
1234567883=19\*43\*1511099  
1234567884=2\*2\*3\*11\*239\*39133  
1234567885=5\*246913577  
1234567886=2\*593\*1040951  
1234567887=3\*7\*7\*8398421  
1234567888=2\*2\*2\*2\*97\*103\*7723  
1234567889=127\*487\*19961  
1234567890=2\*3\*3\*5\*3607\*3803  
1234567891 is a prime number  
1234567892=2\*2\*41\*7527853  
1234567893=3\*13\*31655587  
1234567894=2\*7\*47\*479\*3917  
1234567895=5\*11\*23\*975943  
1234567896=2\*2\*2\*3\*83\*619763  
1234567897=17\*73\*994817  
1234567898=2\*61\*10119409  
1234567899=3\*3\*3\*3\*109\*139831  
1234567900=2\*2\*5\*5\*37\*333667

1234567901=7\*31\*613\*9281  
1234567902=2\*3\*19\*53\*204331  
1234567903=29\*42571307  
1234567904=2\*2\*2\*2\*2\*38580247  
1234567905=3\*5\*443\*185789  
1234567906=2\*11\*13\*631\*6841  
1234567907 is a prime number  
1234567908=2\*2\*3\*3\*7\*4899079  
1234567909=20887\*59107  
1234567910=2\*5\*123456791  
1234567911=3\*411522637  
1234567912=2\*2\*2\*317\*486817  
1234567913 is a prime number  
1234567914=2\*3\*17\*947\*12781  
1234567915=5\*7\*35273369  
1234567916=2\*2\*499\*618521  
1234567917=3\*3\*11\*223\*55921  
1234567918=2\*23\*79\*339727  
1234567919=13\*94966763  
1234567920=2\*2\*2\*2\*3\*5\*59\*87187  
1234567921=19\*64977259  
1234567922=2\*7\*88183423  
1234567923=3\*401\*641\*1601  
1234567924=2\*2\*308641981  
1234567925=5\*5\*193\*255869  
1234567926=2\*3\*3\*3\*43\*107\*4969  
1234567927 is a prime number  
1234567928=2\*2\*2\*11\*1709\*8209  
1234567929=3\*7\*67\*827\*1061  
1234567930=2\*5\*157\*786349  
1234567931=17\*72621643  
1234567932=2\*2\*3\*13\*29\*31\*8803  
1234567933=41\*967\*31139  
1234567934=2\*617283967  
1234567935=3\*3\*5\*27434843  
1234567936=2\*2\*2\*2\*2\*2\*2\*2\*7\*7\*98419  
1234567937=37\*33366701  
1234567938=2\*3\*205761323  
1234567939=11\*112233449  
1234567940=2\*2\*5\*19\*113\*28751  
1234567941=3\*23\*47\*199\*1913  
1234567942=2\*18371\*33601  
1234567943=7\*176366849  
1234567944=2\*2\*2\*3\*3\*17146777  
1234567945=5\*13\*101\*383\*491  
1234567946=2\*617283973  
1234567947=3\*411522649  
1234567948=2\*2\*17\*277\*65543  
1234567949 is a prime number  
1234567950=2\*3\*5\*5\*7\*11\*89\*1201  
1234567951=71\*541\*32141  
1234567952=2\*2\*2\*2\*77160497  
1234567953=3\*3\*3\*45724739  
1234567954=2\*617283977  
1234567955=5\*53\*1721\*2707  
1234567956=2\*2\*3\*349\*294787  
1234567957=7\*176366851  
1234567958=2\*13\*307\*154669  
1234567959=3\*19\*61\*149\*2383  
1234567960=2\*2\*2\*5\*30864199  
1234567961=11\*11\*29\*351829  
1234567962=2\*3\*3\*1663\*41243  
1234567963=31\*173\*211\*1091  
1234567964=2\*2\*7\*23\*659\*2909  
1234567965=3\*5\*17\*137\*35339  
1234567966=2\*617283983  
1234567967 is a prime number  
1234567968=2\*2\*2\*2\*2\*3\*269\*47807  
1234567969=43\*28710883  
1234567970=2\*5\*73\*1691189  
1234567971=3\*3\*7\*13\*397\*3797  
1234567972=2\*2\*11\*359\*78157  
1234567973=131\*283\*33301  
1234567974=2\*3\*37\*41\*135637  
1234567975=5\*5\*233\*211943  
1234567976=2\*2\*2\*139\*1110223  
1234567977=3\*411522659  
1234567978=2\*7\*19\*809\*5737  
1234567979=59\*59\*83\*4273  
1234567980=2\*2\*3\*3\*3\*3\*5\*769\*991  
1234567981 is a prime number  
1234567982=2\*17\*36310823  
1234567983=3\*11\*37411151  
1234567984=2\*2\*2\*2\*13\*13\*456571  
1234567985=5\*7\*7\*97\*51949  
1234567986=2\*3\*205761331  
1234567987=23\*3533\*15193  
1234567988=2\*2\*47\*6566851  
1234567989=3\*3\*137174221  
1234567990=2\*5\*29\*4257131  
1234567991=103\*11986097  
1234567992=2\*2\*2\*3\*7\*311\*23629  
1234567993=1579\*781867  
1234567994=2\*11\*31\*1810217  
1234567995=3\*5\*82304533  
1234567996=2\*2\*67\*4606597  
1234567997=13\*19\*79\*151\*419  
1234567998=2\*3\*3\*4691\*14621  
1234567999=7\*17\*2459\*4219  
1234568000=2\*2\*2\*2\*2\*2\*5\*5\*5\*154321