📄 素数判断.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "素数判断"
ClientHeight = 5235
ClientLeft = 60
ClientTop = 345
ClientWidth = 6705
BeginProperty Font
Name = "Arial Black"
Size = 15.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
LinkTopic = "Form1"
ScaleHeight = 5235
ScaleWidth = 6705
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox txtPOut
Height = 2595
Left = 240
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 6
Top = 2400
Width = 6195
End
Begin VB.TextBox txtTo
Height = 615
Left = 4320
TabIndex = 3
Text = "12346000"
Top = 1560
Width = 2115
End
Begin VB.TextBox txtFrom
Height = 615
Left = 780
TabIndex = 2
Text = "12345000"
Top = 1560
Width = 2295
End
Begin VB.CommandButton Command2
Caption = "筛出指定范围内的所有素数"
Height = 615
Left = 240
TabIndex = 1
Top = 840
Width = 4275
End
Begin VB.CommandButton Command1
Caption = "单个数判断是否素数"
Height = 555
Left = 240
TabIndex = 0
Top = 180
Width = 3315
End
Begin VB.Label Label2
Caption = "至"
Height = 495
Left = 3480
TabIndex = 5
Top = 1620
Width = 435
End
Begin VB.Label Label1
Caption = "自"
Height = 555
Left = 240
TabIndex = 4
Top = 1620
Width = 495
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'素数判断
'曹新国,编写于2004-3-15,最后修改于2006-4-5
'1. 单个素数的判断
'2. 用筛选法求出一定范围内的素数
'234656767 is,12345678 not
Private Sub Command1_Click()
Dim I&, J&, M&
M = InputBox("请输入要判断的数:")
I = 2
J = Sqr(M)
Do While (I <= J) And (M Mod I) <> 0
I = I + 1
Loop
If I > J Then
MsgBox M & "是素数"
Else
MsgBox M & "不是素数,可以被" & Format(I) & "整除,结果为" & Format(M / I)
End If
End Sub
'87651900-87652000: 87651943 87651947 87651979 87651989
Private Sub Command2_Click()
Dim I&, B&, J&, M&, P&, N&
Dim a() As Boolean
Dim flag As Boolean
Dim s$, T$, s1$
'取得FROM和TO
N = Val(txtFrom.Text)
M = Val(txtTo.Text)
'定义数组
ReDim a(N To M)
'先假设都是素数
For I = N To M
a(I) = True
Next I
'将素数的倍数勾掉
For P = 2 To Sqr(M)
B = N + (P - (N Mod P))
If N Mod P = 0 Then B = B - P
If B <= P Then B = B + P
For I = B To M Step P
a(I) = False
Next I
Next P
'在文本框中分行输出结果
I = 0
s = ""
T = ""
For J = N To M
If a(J) Then
s1 = s
s = s & J & ","
If Me.TextWidth(s) > txtPOut.Width - 400 Then
T = T + s1 + vbNewLine
s = Format(J) & ","
End If
End If
Next J
txtPOut = T + s '要加上S,否则会丢掉最后一行的结果
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -