用VB编程:求100到任意数之间的所有素数,并求它们的和。
来源:百度知道 编辑:UC知道 时间:2024/05/18 17:32:31
Private Sub Command1_Click()
Dim I As Long, S As Long, toS As Long
toS = Val(Text1.Text)
For I = 100 To toS
If IsSuShu(I) Then S = S + I
Next
Label1.Caption = "100到" & toS & "素数的和是:" & S
End Sub
Private Function IsSuShu(S As Long) As Boolean
'判读 S 是否是素数
Dim J As Long
For J = 2 To S ^ 0.5
If S Mod J = 0 Then Exit Function
Next
IsSuShu = True
End Function
private sub command1_click()
dim i as integer,c as integer,istep as integer
dim s as string ,n as integer
c=int(text1.text)
istep=iif(c>100,1,-1)
for i=100 to c step istep
if SS(i) then
s=s & " " & i
n=n+i
end if
next
label1.caption="所有100到" & str(c) & "之间的素数:" & s & vbcrlf & "他们的和为:" & str(n)
end sub
function SS(byval i as integer) as boolean
'判断i是否为素数,如果是素数