VB.NET判断质数
Sub Main() 继续: Dim 测时 As New Diagnostics.Stopwatch 测时.Start() '=============================================================================================================== Dim 数 = 1000000000000000000 While 数 <= 1000000000000010000 If 判断质数(数) Then ' And 判断质数(数 + 2) Console.Write("{0} ", 数) End If 数 += IIf(数 Mod 2 > 0, 2, 1) End While '=============================================================================================================== 测时.Stop() Console.WriteLine() Console.WriteLine("{0}毫秒,按任意键继续,按回车结束。", 测时.Elapsed.TotalMilliseconds) If Console.ReadKey(True).Key = ConsoleKey.Enter Then Return Else GoTo 继续 End If End Sub
Function 判断质数(ByVal 数 As Long) As Boolean Dim 除数 As Integer = 2 Dim 开方 As Double = System.Math.Sqrt(数) While 开方 >= 除数 If 数 Mod 除数 > 0 Then 除数 = 除数 + IIf(除数 > 2, 2, 1) Else Return False End If End While Return True End Function
原创文章,作者:xianfajushi的博客,如若转载,请注明出处:https://www.zengqueling.com/vb-net%e5%88%a4%e6%96%ad%e8%b4%a8%e6%95%b0/