VB编程,求代码 输出[100.200]之间所有素数
要求:采用Function过程,输出区间内所有素数以及输出“[100.200]之间共有素数个数...个”,每行输出7个数据。...
要求:采用Function过程,输出区间内所有素数以及输出“[100.200]之间共有素数个数...个”,每行输出7个数据。
展开
展开全部
素数.只能被1和他自己本身整除..整除使用mod函数判断
Dim i As Integer
Dim a As Integer
Dim b As Integer
Dim mb As Boolean
Dim msStr As String
b = 1
For i = 100 To 200
mb = True
For a = 1 To i
If (i Mod a) = 0 Then
If a <> 1 And a <> i Then
mb = False
Exit For
End If
End If
Next a
If mb = True Then
If b <= 7 Then
msStr = msStr & " " & i
b = b + 1
Else
b = 1
msStr = msStr & vbCrLf & " " & i
b = b + 1
End If
End If
Next i
MsgBox msStr
Dim i As Integer
Dim a As Integer
Dim b As Integer
Dim mb As Boolean
Dim msStr As String
b = 1
For i = 100 To 200
mb = True
For a = 1 To i
If (i Mod a) = 0 Then
If a <> 1 And a <> i Then
mb = False
Exit For
End If
End If
Next a
If mb = True Then
If b <= 7 Then
msStr = msStr & " " & i
b = b + 1
Else
b = 1
msStr = msStr & vbCrLf & " " & i
b = b + 1
End If
End If
Next i
MsgBox msStr
展开全部
Private Sub Command1_Click()
Dim i As Integer, s As Integer
For i = 100 To 200
If f1(i) Then
Print i;
s = s + 1
If s Mod 7 = 0 Then Print
End If
Next
Print "个数:";
Print s
End Sub
Public Function f1(x As Integer) As Boolean
Dim n As Integer
n = 1
Do While n < x
n = n + 1
If x Mod n = 0 Then
f1 = False
Exit Do
End If
Loop
If n >= x Then f1 = True
End Function
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Public Function chackSUSU(a As Integer) As Boolean
Dim i As Integer
For i = 2 To a ^ 0.5
If a Mod i = 0 Then chackSUSU = False: Exit Function
Next
chackSUSU = True
End Function
Sub test()
Dim i As Integer
For i = 100 To 200
If chackSUSU(i) Then Debug.Print i
Next
End Sub
Dim i As Integer
For i = 2 To a ^ 0.5
If a Mod i = 0 Then chackSUSU = False: Exit Function
Next
chackSUSU = True
End Function
Sub test()
Dim i As Integer
For i = 100 To 200
If chackSUSU(i) Then Debug.Print i
Next
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询