成都网站建设设计

将想法与焦点和您一起共享

vb.net计算素数,vb用function判断素数

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

显示100以内所有素数 VB.net语言

‘这个是可以选择范围的查找

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编程1到100之间的素数

若a是n的因子,那么n/a也是n的因子,所以如果n有一个大于1的真因子,则必有一个不大于n的1/2次方的因子

yong mod han shu jiu keyi le


网站标题:vb.net计算素数,vb用function判断素数
URL地址:http://chengdu.cdxwcx.cn/article/dscjgje.html