用VB编出由1到100里有多少个素数

要求用两种以上的方法

Private Function sushu(ByVal n As Long) As Boolean
Dim i As Long
For i = 2 To n - 1
If (n Mod i) = 0 Then Exit For
Next i
If i = n Then sushu = True
End Function

Private Sub Form_Click()
Dim a 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的人 ^^

温馨提示:内容为网友见解,仅供参考
第1个回答  2008-03-27
如果一个数a不能被2到a/2的数整除,那么就是素数,或者,如果不能被2到sqr(a)的数整除,也是素数。
所以,判断素数的过程可以编写为:
(1)
Private Function isprime (n As integer) As Boolean
Dim i As integer
isprime = false
For i = 2 To n/2
If n Mod i = 0 Then Exit function
Next i
isprime = True
End Function

(2)
Private Function isprime (n As integer) As Boolean
Dim i As integer
isprime = false
For i = 2 To sqr(n)
If n Mod i = 0 Then Exit function
Next i
isprime = True
End Function

主程序可以编写为:
Private Sub Form_Click()
Dim i As integer, num as integer
For i = 1 To 100
If isprime(i) Then
print i;
num = num + 1
if num mod 5 = 0 then print
End If
Next i
print
Print "从1到100共有" & num & "个素数"
End Sub

用VB编出由1到100里有多少个素数
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、输出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语言:求100以内的所有素数,统计个数,并按每行5个输出!求代码!
共有25个素数。Public Class Form1 Dim n As Int16 = 0 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Text = PrimeNumber(2, 100)MsgBox("共有素数:" & n & "个。")End Sub Function PrimeNumber(ByVal sta...

vb~~1到100的素数的代码
47 53 59 61 67 71 73 79 83 89 97 === 代码:=== Option Explicit Private Sub Command1_Click()Dim i As Integer, strPrime As String, index As Integer For i = 1 To 100 If isPrime(i) Then If strPrime = "" Then strPrime = CStr(i)ElseIf index Mod 5 = ...

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 里面的素数
Private Sub Command1_Click() For i = 1 To 100 For j = 2 To i - 1 If i Mod j = 0 Then Exit For Next j If j > i - 1 And i <> 1 Then Print i; n = n + 1 If n Mod 5 = 0 Then Print End If Next i End Sub 追问: 定义PRINT 回答: 你想问什...

vb求100以内的素数的个数
Count,i s_Count=0 '素数个数 For i=2 To 100 s_Count=s_Count+isSushu(i)Next MsgBox s_Count Function isSushu(num) '判断是否素数,是返回1,否返回0 Dim II For II=2 To Num\\2 If (Num Mod II)=0 Then isSushu=0 Exit Function End If Next isSushu=1 End Function ...

VB编程:输出1~100之间所有素数
'例子需控件: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 = ...

如何用vb子程序输出素数
示例代码,求1000以内的素数:Option Explicit Dim I As Integer Dim j As LongPrivate Function IsPrime(ByVal n As Integer) As Boolean On Error Resume Next Dim I As Integer Dim temp As Integer Dim flag As Boolean flag = True If (n <= 1) Then IsPrime...

用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

相似回答
大家正在搜