作者meto000 (阳货欲见孔子)
看板Visual_Basic
标题Re: [VB6 ] 找出范围内的质数,想请各位帮忙缩短...
时间Fri Oct 2 14:05:55 2009
※ 引述《costbook (没$结婚干嘛,有$干嘛结婚)》之铭言:
: '找出范围内所有整数质数
: 'VB .NET
: Dim nonprimes(0) As Boolean
: Sub getAllPrime(ByVal top As Integer)
: ReDim nonprimes(top)
: Dim value As New MyInt(Of Integer)
: nonprimes(0) = True
: nonprimes(1) = True
: Dim i As Integer = 2
: '用筛法标记prime和nonprime
: While i <= top / i
: '这边用myInt class,速度会比用isPrime() function慢
: value.setting(i)
: If value.isPrime() Then
: For j As Integer = 2 To top / i
: If j * i <= top Then
: nonprimes(j * i) = True
: End If
这段有点不明白
既然 i <= top / i
又 j = 2 ~ top / i
那 i*j 一定会 <= top 才对
那就不用判断 If j * i <= top 了
直接下 nonprimes(j * i) = True 不就好了?
: Next
: End If
: i += 1
: End While
: End Sub
--
迷时师渡 悟时自渡
~ 六祖惠能
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 210.241.123.132
1F:推 costbook:这是我一年级写的作业,我也忘记为什麽了 10/02 15:38