VB程序设计----函数与过程

2024-11-26 22:30:41
推荐回答(2个)
回答1:

Private Sub Command1_Click()
Dim I As Long, n As Long, Gen As Long, Su() As Long, S As Long

n = 100 '查找 100 以内素数,记忆到数组 Su()
For I = 2 To n
Gen = Sqr(I) '记升祥忆 I 的平方根
For J = 1 To S '用 I 除以没橘已经找到的素数
If I Mod Su(J) = 0 Then GoTo NextI '能整除,不是素数,检查下一个
If Su(J) > Gen Then Exit For '检测到大于 I 的平方根枯笑团就不用查了。删除此语句,结果一样,但速度慢得多
Next
S = S + 1: ReDim Preserve Su(S): Su(S) = I
NextI:
Next

'显示所有的孪生素数
Me.Cls: Me.AutoRedraw = True
For I = 2 To S
If Su(I) - Su(I - 1) = 2 Then
Me.Print Su(I - 1) & " " & Su(I)
End If
Next
End Sub

回答2:

提问还嫌樱核是给点分的好,都没人帮你,我帮你芹掘把

Option Explicit

Sub test()
Dim i As Integer, j As Integer, n As Integer
Dim arr() As Integer, tem As String
Dim ifs As Boolean
For i = 2 To 100
ifs = True
For j = 2 To i - 1
If i Mod j = 0 Then
ifs = False
Exit For
End If
Next j
If ifs = True Then
n = n + 1
ReDim Preserve arr(1 To n)
arr(n) = i
End If
Next i
For i = 1 To UBound(arr) - 1
If arr(i + 1) - arr(i) = 2 Then tem = tem & arr(i) & "颂伍," & arr(i + 1) & vbCrLf
Next i
Print tem
End Sub