作者keria (住左住右超好看)
看板Visual_Basic
标题Re: [VB2005][心得] 关於1-1000的质数
时间Wed Aug 9 22:32:39 2006
<恕删>
记得之前上数学课时,
有提到求质数的方式,
就是小於该数的平方根的质数无法整除时,
它也会是个质数。
基於此概念,
写了一个如下的程式,
请在VB.Net(2003)的Application中,
程式使用一个TextBox、Button、ListBox:
===程式段开始===
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim aryPrime(0) As Double
Dim sqrtNum As Double
Dim Index1 As Double, Index2 As Double
aryPrime(0) = 2
For Index1 = 3 To CDbl(TextBox1.Text)
sqrtNum = Fix(Math.Sqrt(Index1))
For Index2 = 0 To UBound(aryPrime)
If aryPrime(Index2) <= sqrtNum Then
If (Index1 Mod aryPrime(Index2)) = 0 Then
Exit For
End If
Else
If Index2 = UBound(aryPrime) Then
ReDim Preserve aryPrime(UBound(aryPrime) + 1)
aryPrime(UBound(aryPrime)) = Index1
Exit For
End If
End If
Next Index2
Next Index1
ListBox1.Items.Clear()
For Index1 = 0 To UBound(aryPrime)
ListBox1.Items.Add(aryPrime(Index1))
Next
End Sub
===程式段结束===
其中,TextBox输入一个大於3的数,
它就会在ListBox中列出1~输入数的所有质数。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.160.53.1
1F:推 dong2:虽然现在还看不懂 @"@ 不过...先带回家以後再慢慢看好了XD 08/09 22:42
2F:推 dong2:还有~我数学真的很烂~所以只能土法炼钢orz 08/09 22:45
3F:推 dong2:k大感谢您的分享,我日後会好好研究研究的,等研究所考完orz 08/09 22:47