VB程序输出100内所有差为二的素数。

2024-12-17 06:01:31
推荐回答(4个)
回答1:

Private Sub Command1_Click()
Dim a() As Integer
Dim k As Integer, m As Integer, i As Integer, j As Integer
k = 0
For m = 2 To 100
i = 2
Do While m Mod i <> 0 '判断是否是素数
i = i + 1
Loop
If i = m Then '是素数
ReDim Preserve a(k) '不知道数组的长度应该用redim定义重新范围数组
'ReDim Preserve 进行对数组赋值
a(k) = m
k = k + 1
End If
Next m
For j = LBound(a()) To UBound(a()) - 1
If a(j + 1) - a(j) = 2 Then
Print a(j + 1); a(j) & "这两个素数就是一对孪生素数"
End If
Next j
End Sub

回答2:

差为2? 啥意思

回答3:

Private Sub Command1_Click()
Dim a()
For i = 100 To 1 Step -1
For j = i - 1 To 1 Step -1
If i Mod j = 0 Then n = n + 1
Next
If n = 1 Then
m = m + 1
ReDim Preserve a(m)
a(m) = i
If a(m - 1) - a(m) = 2 Then Print a(m) & Space(1) & a(m - 1) & vbCrLf
End If
n = 0
Next
End Sub

回答4:

ck()
Dim A(100) As Integer
Dim N As Integer
Dim i As Integer, j As Integer, x As Integer
For i = 2 To 100
x = 0
For j = 2 To i - 1
If i Mod j = 0 Then x = 1
Next j
If x = 0 Then
A(N) = i
N = N + 1
End If
Next i

For i = 0 To N - 1
If A(i + 1) - A(i) = 2 Then
Print A(i) & ";" & A(i + 1)
End If
Next

End Sub