VB语言:求100以内的所有素数,统计个数,并按每行5个输出!求代码!
共有25个素数。
Public Class Form1
Dim n As Int16 = 0
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = PrimeNumber(2, 100)
MsgBox("共有素数:" & n & "个。")
End Sub
Function PrimeNumber(ByVal startNum As Long, ByVal endNum As Long) As String
Dim str As String = ""
Dim i As Long
Dim j As Long
Dim yn As Boolean = True
For j = startNum To endNum
For i = 2 To Int(Math.Sqrt(j))
If j Mod i = 0 Then GoTo nxt
Next
n += 1
If n Mod 5 = 0 Then
str = str & j & vbCrLf
Else
str = str & j & ","
End If
nxt:
Next
Return str
End Function
End Class
Dim i, j, n As Integer
For i = 2 To 100
For j = 2 To Int(Sqr(i))
If i Mod j = 0 Then Exit For
Next j
If j > Int(Sqr(i)) Then
Print i;
n = n + 1
If n Mod 5 = 0 Then Print
End If
Next i
Print vbCrLf & "100以内素数总数=" & n
End Sub