编写一个函数,其功能为判断某个整数是不是素数,并用其在一个文本框内...
发布网友
发布时间:2024-10-13 12:51
我来回答
共4个回答
热心网友
时间:2024-10-14 16:49
Private Sub Command1_Click()
Text1 = ""
For i = 1000 To 10000
If isprime(i) Then
Text1 = Text1 & i & " "
j = j + 1
If j Mod 10 = 0 Then Text1 = Text1 & vbCrLf
End If
Next
End Sub
Private Function isprime(ByVal n As Integer) As Boolean
isprime = True
For i = 2 To Sqr(n)
If n Mod i = 0 Then isprime = False: Exit For
Next
End Function
热心网友
时间:2024-10-14 16:47
Option Explicit
Function IsPrime(num As Integer) As Boolean
Dim i As Integer
IsPrime = True
For i = 2 To num - 1
If num Mod i = 0 Then IsPrime = False: Exit For
Next
End Function
Private Sub Form_Load()
Dim s As String, j As Integer, i As Integer
For i = 1000 To 10000
If IsPrime(i) Then
j = j + 1
s = s & i & IIf(j Mod 10 = 0, vbCrLf, " ")
End If
Next
Text1.Text = s
End Sub
热心网友
时间:2024-10-14 16:48
Private Sub Command1_Click() ‘
For i = 200 To 300
For j = 2 To i - 1
If i Mod j = 0 Then Exit For
Next j
If j = i Then List1.AddItem i ‘发现数学特色,如果是素数,最后的这个j可以达到i的程度
Next i ‘或者参照课本P93,用设置标记的方法。设b=true
End Sub
热心网友
时间:2024-10-14 16:51
Private Sub Command1_Click()
Dim i As Integer, k As Integer, t As String
For i = 1000 To 10000
If ss(i) Then
t = t & Format(i, "@@@@@")
k = k + 1
If k Mod 10 = 0 Then
t = t & vbCrLf
End If
End If
Next
Text1 = t
End Sub
Private Function ss(ByVal x As Integer) As Boolean
Dim j As Integer
For j = 2 To Int(Sqr(x))
If x Mod j = 0 Then
Exit For
End If
Next
If j > Int(Sqr(x)) Then ss = True
End Function