vb.net素数 vb求100以内的素数及和代码
显示100以内所有素数 VB.net语言
‘这个是可以选择范围的查找
10余年的兴和网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站的优势是能够根据用户设备显示端的尺寸不同,自动调整兴和建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“兴和网站设计”,“兴和网站推广”以来,每个客户项目都认真落实执行。
Public Class Form1
'一个大于1的自然数,除了1和它本身外,不能被整除以其他自然数(质数),换句话说就是该数除了1和它本身以外不再有其他的因数
' 章鱼哥 3107073263
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim PD As Boolean = True
TextBox1.Clear() '首先清空文本框,这里面将放置符合条件的素数
If NumericUpDown1.Value NumericUpDown2.Value Then '从小到大
For i = NumericUpDown1.Value To NumericUpDown2.Value '循坏判断所有数
For j = 2 To i - 1 '内循环是除了1和它本身的
If i Mod j = 0 Then '判断是否能被除了1和自身以为的 比自身小的数整除
PD = False '如果能被整除,将PD 设置为False
Exit For '退出里层For 循环
Else
PD = True
End If
Next
If PD Then '如果PD为True ,则为素数
TextBox1.Text = i " "
End If
Next
End If
End Sub
End Class
vb.net利用doloop until 计算从1开始的素数之和直到和不大于989为止
Module Module1
Sub Main()
Dim sum As Integer = 0
Dim Bound As Integer = 989
Dim num As Integer = 0
Dim cprime As Integer = 0
Do
num += 1
If IsPrame(num) Then
sum += num
cprime = num
End If
Loop Until sum Bound
Console.WriteLine(sum - cprime)
Console.ReadKey()
End Sub
'判断一个数是不是素数
Public Function IsPrame(ByVal num As Integer) As Boolean
If num = 1 Then Return False '素数的定义是大于1
For i = 2 To CType(Int(Math.Sqrt(num)), Integer)
If num Mod i = 0 Then Return False
Next
Return True
End Function
End Module
vb语言,输入两个数,输出这两个数中间所有的素数
同意楼上的见解,
我在VB6.0中测试没有问题,能看到输出,VB.NET中也没有问题。
不过是否还有点得注意:
Print j 这句是把找到的素数以窗体的前景色(ForeColor)显示在窗体上,你指的没有输出,是指在窗体上看不到内容,是吧?是否你的窗体的前景色(ForeColor)与背景色(BackColor)相同或近似相同,造成视觉错误呢?
vb.net的编程
Option Strict Off
Option Explicit On
Friend Class Form1
Inherits System.Windows.Forms.Form
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
Dim j As Integer
Dim A(9) As Short
Dim D(9) As Short
Dim i As Short
Dim B As Short
Dim C As Short
Dim F As Short
Dim G As Boolean
Dim H() As Short
Dim K As Short
Dim S As String
C = 32767
For i = 1 To 10
A(i - 1) = CShort(InputBox("输入第 " i "个数"))
If B A(i - 1) Then B = A(i - 1)
If C A(i - 1) Then C = A(i - 1)
D(i - 1) = Int(Rnd() * 100 + 0.5)
If F D(i - 1) Then F = D(i - 1)
Next i
For i = 2 To 100
For j = 2 To Int(System.Math.Sqrt(i) + 0.5)
'UPGRADE_WARNING: Couldn't resolve default property of object j. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
'UPGRADE_WARNING: Mod has a new behavior. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
If i Mod j 0 Then
G = True
Else
G = False
Exit For
End If
Next j
If G = True Then
ReDim Preserve H(K)
H(K) = i
K = K + 1
End If
Next i
S = "2;"
For i = 0 To K - 1
S = S H(i) ";"
Next i
MsgBox("最大数为:" B ";最小数为:" C)
MsgBox("最大的随机数为:" F)
MsgBox("1--100之间共有" K + 1 "个素数:" S)
End Sub
End Class
vb判断素数需要添加哪些控件
vb判断素数需要添加的控件有button和listbox。
1、打开vb工程,在上面建一个窗体,然后创建button和listbox
2、在findPrime上写入如下代码:
Private Sub CommandButton1_Click()
Dim flag As Boolean
flag = True
ListBox1.AddItem (1) '默认1和2都是素数
ListBox1.AddItem (2)
For i = 1 To 1000
For J = 2 To i - 1
If (i Mod J = 0) Then
flag = True
Exit For
Else
flag = False
End If
Next
If flag = False Then
ListBox1.AddItem (i)
End If
Next i
End Sub
3、运行结果,列出了1-1000内的所有素数。
VB:求出100以内所有素数的和,并将结果输出到窗体上,并存入所给变量SUM中
Private Sub Command1_Click()
Dim i As Integer
Dim sum As Integer
For i = 1 To 100
If Prime(i) Then
sum = sum + i
End If
Next
Print "100以内所有素数的和是" sum
End Sub
Private Function Prime(ByVal n As Integer) As Boolean
Dim i As Integer
If n 2 Then
Prime = False
Exit Function
End If
For i = 2 To Sqr(n)
If n Mod i = 0 Then
Prime = False
Exit Function
End If
Next
Prime = True
End Function
本文标题:vb.net素数 vb求100以内的素数及和代码
文章源于:http://scyanting.com/article/hjddcj.html