首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

求诸位大神帮帮忙

2012-08-25 
求各位大神帮帮忙老师布置的作业不会啊求各位帮帮忙用VB统计1——1000之间的素数,并注意进度条[解决办法]作

求各位大神帮帮忙
老师布置的作业不会啊 求各位帮帮忙 
用VB统计1——1000之间的素数,并注意进度条

[解决办法]
作业自己做
[解决办法]
知道素数的算法吧?写个方法用来判断是否是素数,然后循环传递1到1000进去,进度条最大值为1000,当前值为当前循环值。代码就自己敲下吧。
[解决办法]
自己写吧,这个也不是很难

[解决办法]
我倒是新写了一个现成的,问题是你敢不敢拿这个交作业?

Form代码

VB code
Option ExplicitPrivate priEvent_Index As LongPrivate priEvent_Title As StringPrivate Sub Command1_Click()  Dim tHash() As Byte  Dim tPrimes() As Long    PrimeNumbers_Create_Buffer 10000000, tHash(), tPrimes()  Text1.Text = UBound(tHash()) + 1 & " " & UBound(tPrimes()) + 1    End SubPublic Sub PrimeNumbers_Create_Buffer(ByVal pScan_Count As Long, ByRef pHash() As Byte, ByRef pPrimesList() As Long, Optional ByVal pPrimesList_Buffer_Size As Long = &H10000)  Dim tHash_Length As Long    Dim tPrimesList_Count As Long  Dim tPrimesList_Buffer_Length As Long    Dim tScan_Index As Long  Dim tScan_Length As Long    Dim tSiev_Index As Long  Dim tSiev_Start As Long  Dim tSiev_Bound As Long    Dim tEvent_Count As Long  Dim tEvent_Index As Long  Dim tEvent_Timer As Long    tHash_Length = pScan_Count \ 8 + (CBool(pScan_Count Mod 8) And 1)  tPrimesList_Buffer_Length = pPrimesList_Buffer_Size - 1  tSiev_Bound = Sqr(pScan_Count)  tScan_Length = pScan_Count    ReDim pHash(tHash_Length)  ReDim pPrimesList(tPrimesList_Buffer_Length)    Bits_SetToBytes_Buffer pHash(), 0, 1  Bits_SetToBytes_Buffer pHash(), 1, 1    For tScan_Index = 2 To tSiev_Bound    If Not CBool(Bits_GetByBytes_Buffer(pHash(), tScan_Index)) Then            PrimeList_Append pPrimesList(), tScan_Index, tPrimesList_Count, tPrimesList_Buffer_Length, pPrimesList_Buffer_Size                 tSiev_Start = tScan_Index ^ 2            priEvent_Title = " SiveScaning:" & (tScan_Index / tSiev_Bound) * 100 & " Sieving:"            For tSiev_Index = tSiev_Start To tScan_Length Step tScan_Index                  Bits_SetToBytes_Buffer pHash(), tSiev_Index, 1                        Working_View tSiev_Index, pScan_Count                If &H7FFFFFFF - tSiev_Index < tScan_Index Then Exit For      Next        End If          Next    Open "PrimeHash.dat" For Binary As #1    Put #1, 1, pHash()  Close #1    priEvent_Title = "Sive Outing:"    For tScan_Index = pPrimesList(tPrimesList_Count - 1) + 2 To tScan_Length Step 2    If Not CBool(Bits_GetByBytes_Buffer(pHash(), tScan_Index)) Then             PrimeList_Append pPrimesList(), tScan_Index, tPrimesList_Count, tPrimesList_Buffer_Length, pPrimesList_Buffer_Size                     If &H7FFFFFFF - tScan_Index < 2 Then Exit For           End If        Working_View tScan_Index, tScan_Length      Next    ReDim Preserve pPrimesList(tPrimesList_Count - 1)  Open "PrimeList.dat" For Binary As #1    Put #1, 1, pPrimesList()  Close #1End SubPublic Sub Working_View(ByRef pWork_Init As Long, ByRef pWork_All As Long)    priEvent_Index = priEvent_Index + 1        If priEvent_Index > 1000000 Then    Form1.Text1 = priEvent_Title & (pWork_Init / pWork_All) * 100    priEvent_Index = 0    DoEvents  End IfEnd SubPublic Sub PrimeList_Append(ByRef pPrime_List() As Long, ByRef pPrime_Value As Long, ByRef pPrime_Index As Long, ByRef pPrime_Length As Long, ByRef pLength_Step As Long)  pPrime_List(pPrime_Index) = pPrime_Value  pPrime_Index = pPrime_Index + 1        If pPrime_Index > pPrime_Length Then    pPrime_Length = pPrime_Length + pLength_Step    ReDim Preserve pPrime_List(pPrime_Length)  End IfEnd SubPrivate Sub Form_Load()  Bits_ValueListInitEnd Sub
------解决方案--------------------


LS你太强大了.

你是哪里的神仙妹妹,有庙门没,我的烧香
[解决办法]
上面给你的代码是计算2147483648以内素数的。

下面是上面的代码的简化版。用到的算法不是通常那种一个一个数字取余排除的,而是“埃拉托斯特尼筛法”。

筛选1000以内素数,可得到168个。

VB code
Private Sub Command2_Click()  Dim tPrimes() As Long    ReDim tPrimes(1023)    PrimeSive tPrimes(), 1000    Debug.Print UBound(tPrimes()) + 1 '返回素数数量(由于是从0计算的,要加1)End SubPublic Sub PrimeSive(ByRef pPrimes() As Long, ByVal pPrimeBound As Long)    Dim tSiveList() As Byte   '筛表载体  Dim tSiveIndex As Long  Dim tSiveLength As Long    Dim tScanIndex As Long    Dim tPrimeIndex As Long    ReDim tSiveList(pPrimeBound)  tSiveList(0) = 1: tSiveList(1) = 1    tSiveLength = Sqr(pPrimeBound)    '生成筛表同时导出素数    For tSiveIndex = 2 To tSiveLength        If Not CBool(tSiveList(tSiveIndex)) Then            PrimeAppend pPrimes(), tPrimeIndex, tSiveIndex            For tScanIndex = tSiveIndex ^ 2 To pPrimeBound Step tSiveIndex              tSiveList(tScanIndex) = 1              Next          End If  Next    '从筛表里导出素数    For tSiveIndex = pPrimes(tPrimeIndex - 1) + 2 To pPrimeBound Step 2      If Not CBool(tSiveList(tSiveIndex)) Then            PrimeAppend pPrimes(), tPrimeIndex, tSiveIndex                End If      Next  '修整输出素数表  ReDim Preserve pPrimes(tPrimeIndex - 1)End SubPublic Sub PrimeAppend(ByRef pPrimes() As Long, ByRef pPrimeIndex As Long, ByVal pPrime As Long)  '追加素数到素数表  Dim tPrimes_Buffer_Length As Long    tPrimes_Buffer_Length = UBound(pPrimes())    If tPrimes_Buffer_Length < pPrimeIndex Then        ReDim Preserve pPrimes(tPrimes_Buffer_Length + 1024)    End If    pPrimes(pPrimeIndex) = pPrime    pPrimeIndex = pPrimeIndex + 1End Sub 

热点排行