📄 求素数个数.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "例[8-13]求素数的个数"
ClientHeight = 1080
ClientLeft = 60
ClientTop = 345
ClientWidth = 3870
LinkTopic = "Form1"
ScaleHeight = 1080
ScaleWidth = 3870
StartUpPosition = 3 '窗口缺省
Begin VB.Label Label2
BorderStyle = 1 'Fixed Single
Height = 435
Left = 2535
TabIndex = 1
Top = 360
Width = 1095
End
Begin VB.Label Label1
Caption = "500~1999之间的素数个数:"
Height = 375
Left = 248
TabIndex = 0
Top = 413
Width = 2895
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Form_Click()
Dim i As Integer, num As Integer
num = 0
For i = 500 To 1999
If (isprime(i) And (i Mod 100) \ 10 = 7) Then num = num + 1
Next i
Label2.Caption = Str(num)
End Sub
Private Function isprime(k As Integer) As Boolean
'本函数isprime判断正整数k是否为素数,函数返回值为Boolean类型
Dim i As Integer
isprime = True '先假设该数为素数,isprime赋值为True
If (k <= 1) Then
isprime = False
Exit Function
End If
For i = 2 To k - 1
If (k Mod i = 0) Then '如果该数能被2~k-1之间的某一个数整除,
isprime = False '则该数不是一个素数,isprime赋值为False
Exit Function
End If
Next i
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -