推 costbook:這是我一年級寫的作業,我也忘記為什麼了 10/02 15:38
※ 引述《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