VB程序 求100内所有素数的和。18
发布网友
发布时间:2023-10-10 04:10
我来回答
共4个回答
热心网友
时间:2024-10-28 16:57
Dim m As Integer
Dim i As Integer
Dim sum As Integer
sum = 0
For m = 2 To 100
For i = 2 To m - 1 '判断是否是素数
If m Mod i = 0 Then
Exit For
End If
Next i
If i = m Then '如果是,相加
sum = sum + m
End If
Next m
Print sum
另外楼主的代码有误,按你那个思路,代码如下
Dim sum As Integer
For m = 2 To 100
i = 2
Do While m Mod i <> 0
i = i + 1
Loop
If i = m Then sum = sum + m
Next m
Print s
热心网友
时间:2024-10-28 16:57
Dim s As Integer
For m=2 to100
Do While m mod i<>0
i=i+1
Loop
If i=m then s=s+m
next i
print s
大概不需要解释吧。
热心网友
时间:2024-10-28 16:58
if i =m 那里的i 是2 to m-1。因此语句是错误的
热心网友
时间:2024-10-28 16:58
Private Sub Command1_Click()
For m = 2 To 100
i = 2
Do While m Mod i <> 0
i = i + 1
Loop
If i = m Then s = s + m
Next m
Print s
End Sub