VB编程:输出1~100之间所有素数

如题.

查找1到1000的素数显示到文本框,瞬间完成
'例子需控件:Command1、Text1

Private Sub Command1_Click()
Dim a() As Long, nStr As String

'Call FindSuShu(18, 100, a, 5) '查找大于17的5个素数存入数组a
Call FindSuShu(1, 1000, a) '查找1到1000之间的所有个素数

'显示到 Text1
For I = LBound(a) To UBound(a)
nStr = nStr & a(I) & " "
Next
Text1.Text = nStr
End Sub
Private Sub FindSuShu(FromS As Long, ToS As Long, Su() As Long, Optional Ge As Long)
'查找位于 FromS 和 ToS 之间的质数(素数),存入数组 Su()
'Ge 表示查找个数, 如果指定 Ge,则忽略 ToS
Dim I As Long, J As Long, SuAll() As Long, S As Long, MaxN As Long
Dim ReS As Long

S = 1
ReDim SuAll(1 To 1): SuAll(1) = 2

ReDim Su(1 To 1)
If FromS <= 2 And ToS >= 2 Then ReS = 1: Su(1) = 2

I = 2
Do
DoEvents
I = I + 1
If Ge < 1 And I > ToS Then Exit Do
MaxN = I ^ 0.5 '比较 I 的一半
For J = 1 To S
If I Mod SuAll(J) = 0 Then GoTo NextI '不是质数
If SuAll(J) > MaxN Then Exit For '是质数
Next J

S = S + 1 '找到质数的总个数
ReDim Preserve SuAll(1 To S)
SuAll(S) = I
If I < FromS Then GoTo NextI
ReS = ReS + 1 '返回质数的总个数
ReDim Preserve Su(1 To ReS)
Su(ReS) = I
If Ge > 0 And ReS >= Ge Then Exit Do
NextI:
Loop
End Sub
'来源:我的QQ http://new.qzone.qq.com/32063270/blog/1222349880
温馨提示:内容为网友见解,仅供参考
第1个回答  2008-10-07
Dim a As Double
Dim b As Double
Dim c As Double
Dim d As Double
Dim t As Double
Dim Uu9 As String
Private Sub Form_Click()
a = 1
b = 100
If b > 90000 Then
daan = MsgBox("判断大于90000的数将用较长时间" + vbCrLf + " 继续吗?", 36, "询问")
If daan = vbNo Then
Exit Sub
End If
End If
If a >= b Then
t = a
a = b
b = t
'MsgBox "第一个数应比第二个数小!", 16, "数据错误"
'Exit Sub
End If
If a <= 2 Then
Uu9 = "2"
a = 2
End If
For c = a To b
bz = False
If c / 2 = c \ 2 Then
bz = True
End If

For d = 3 To Int(Sqr(c)) Step 2
If c / d = c \ d Then
bz = True
d = c
End If
DoEvents
Next d
If bz = False Then
Uu9 = Uu9 & " " & Str(c)
End If
Next c
Print Uu9
End Sub

Private Sub Form_Load()
Me.Width = 10000
Me.Height = 1000
End Sub
第2个回答  2008-10-07
dim i as integer,j as integer
dim b as boolean

for i=2 to 100
b=true
for j=2 to int(sqrt(i))
if i mod j=0 then b=false:exit for
next
if b then print i & " "
next本回答被网友采纳
第3个回答  2008-10-07
Function Sushu(Nums As Integer) As Boolean '判断是否是素数
Sushu = False
For i = 2 To Sqr(Nums)
If Nums Mod i = 0 Then
Exit Function
End If
Next i
Sushu = True
End Function

Private Sub Form_Load()
Dim i As Integer
For i = 1 To 100
If Sushu(i) = True Then
MsgBox i'你也可以换成你自己的输出方式
End If
Next i
End Sub

VB编程:输出1~100之间所有素数
Private Sub Command1_Click()Dim a() As Long, nStr As String 'Call FindSuShu(18, 100, a, 5) '查找大于17的5个素数存入数组a Call FindSuShu(1, 1000, a) '查找1到1000之间的所有个素数 '显示到 Text1 For I = LBound(a) To UBound(a)nStr = nStr & a(I) & " "Next ...

如何在VB中输出素数?
1、输出1-100以内的素数:2、同样,也是输出1-100以内的素数,这个构造一个数组,将其所有元素初始化为1,表示素数,这时取x从2开始,到100以内做循环。若x为素数,即prime[x]!=0,使得每个i*x位置元素置0(prime[i*x]=0),表示为非素数,循环结束前x+1。3、该方法是输出100个素数,基本...

vb~~1到100的素数的代码
strPrime = strPrime & vbTab & CStr(i)End If index = index + 1 End If Next i Print strPrime End Sub Private Function isPrime(ByVal n As Integer) As Boolean If n = 1 Then isPrime = True ElseIf n > 1 Then Dim flag As Boolean, i As Integer flag = True For i = ...

VB编写程序找出1到100之间的所有孪生素数。要求包含一个判断素数的函数...
" & i+2 & "是一对孪生素数"\\x0d\\x0a End If\\x0d\\x0aNext i\\x0d\\x0aEnd Sub

用VB编出由1到100里有多少个素数
As String For i = 1 To 100 If sushu(i) = True Then a = a & i & " , "Else End If Next a = a & "以上是1~100 之间素数的集合"Print a End Sub Private Sub Form_Load()Me.Move 0, 0, Screen.Width, Screen.Height End Sub 参考资料:最恨VB是一个最爱VB的人 ^^ ...

VB编程 输出1到100之间所有数?
以下是使用VB编程语言输出1到100之间的所有数的程序:Dim i As Integer For i = 1 To 100 Debug.Print i Next i 该程序使用For循环遍历1到100之间的所有整数,并使用Debug.Print语句将每个整数输出到“立即窗口”(Immediate Window)中。你也可以使用MsgBox语句将每个整数输出到一个消息框中,或者将...

编写打印1-100之间的全部素数,要求每行显示十个数。谁帮我写下这短程...
if c=1 ?? str(i,4)h=h+1 if h%10=0 ?endif endif endfor set talk on return VB编写:Private Sub Form_Load()Show Dim i As Integer, j As Integer, h As Integer, c As Integer h = 0 For i = 1 To 100 c = 1 For j = 2 To Int(Sqr(i))If i Mod j = 0 Then...

用VB编程输出1到100的素数,用简单一点的那种,谢啦
Dim i As Long, j As Long, k As Long For i = 1 To 100 k = Int(Sqr(i))For j = 2 To k If i Mod i = 0 Then Exit For Next j If j > k Then Print j Next i

vb求100以内的素数
Me.Cls Dim yn As Boolean '为真表示是素数 Dim i As Integer, j As Integer, x As Integer For i = 1 To 100 yn = True '为真表示是素数,否则就不是,先默认为是素数 For j = 2 To i - 1 '素数要大小1,所以循环从2开始,素数不包括本身,所以循环终值减小1 If i Mod ...

用VB 编程 找出1-100 里面的素数
print在窗体打印输出 追问: VB2008 就是你现在写的 程序 输进去,他说 Print 未定义 应该对 它function否则不能用啊 回答: 我这是VB6.0的,VB2008不能直接用print输出在窗体,下面是vb2008写的代码,结果在名为label1的 标签 中输出 Public Class Form1 Private Sub Form1_Load(ByVal...

相似回答