素数应用程序的输出错误

时间:2014-03-14 01:20:26

标签: visual-studio-2012 primes

我正在使用.NET Framework 4在visual basic中创建一个应用程序。我必须根据用户的输入生成素数列表。到目前为止,对于我的输出,如果你输入前五个素数的5,你得到3 5 7 7 9 11 11.我不确定我的数字增量是否在错误的位置。感谢你给与我的帮助。另外,我不确定如何在我的代码中包含2作为素数。

Imports System.Math
Public Class Form1
Dim number, divisor, max, count As Integer
Dim IsPrime As Boolean
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    number = TextBox1.Text
    For divisor = 2 To Sqrt(number)
        If number Mod divisor = 0 Then
            IsPrime = False
            TextBox2.Text = ("Number is not prime")
            Exit For
        Else
            TextBox2.Text = ("Number is prime")
        End If
    Next
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim Wrap As String
    Wrap = Chr(13) & Chr(10)
    max = TextBox3.Text
    Dim count = 0
    number = 2
    While count <= max
        IsPrime = True

        For divisor = 2 To Sqrt(number)
            If number Mod divisor = 0 Then
                IsPrime = False
                Exit For
            Else
                IsPrime = True
                TextBox4.Text += number & Wrap
                count += 1
            End If
        Next
        number += 1
    End While
End Sub
End Class

2 个答案:

答案 0 :(得分:1)

你不应该在for循环中使用else分支,在这种情况下,每次Mod不等于0时,你将触摸else块,例如取11号:

  

11 mod 2&lt;&gt; 0,你进入了else块,

     

11 mod 3&lt;&gt; 0,你再次进入了其他区块!

您可以将数字除以2到sqrt(数字)之间的所有数字,然后使用isprime检查它是否是这样的素数。(我在这里使用VBScript)

isprime = true
for i = 2 to Int(sqr(number))
    if number mod i = 0 then
        isprime = false
        exit for
    end if
next

if isprime = true then
    count = count + 1
    ' do something here...
end if

number = number + 1

并且不要忘记截断数字的平方根。

答案 1 :(得分:0)

与您分享此脚本:

Option Explicit
Dim Title,Copyright,j,fso
Dim WshShell,Affich,LogFile
Title = "Calcul Nombres Premiers"
Copyright = " (c) by Hackoo 2015"
For j = 2 to 1000
    If Premier(j) = True Then
        Affich = Affich & j & vbTab
    End If
Next
MsgBox Affich,vbInformation,Title + Copyright
LogFile = "c:\NombresPremiers.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(LogFile) Then
    fso.DeleteFile LogFile
end If
Affich = replace(Affich,vbTab,vbCrlf)
Call WriteLog(Affich,LogFile)
Set WshShell=CreateObject("wscript.shell")
WshShell.Run LogFile
'**********************************
Function Premier(Nombre)
    Dim i,d
' Trois nombres ne seront pas pris en compte par le compteur,
' on s'organise pour qu'ils soient vus avant.
    Select Case Nombre
    Case 0
        Premier = False
        Exit Function
    Case 1
        Premier = False
        Exit Function
    Case 2
        Premier = True
        Exit Function
    End Select
    For i = 2 To Int(Sqr(Nombre)) + 1
        d = Nombre Mod i
        If d = 0 Then
            Premier = False
            Exit Function
        End If
    Next 
    Premier = True
End Function
'**********************************
Sub WriteLog(strText,LogFile)
    Dim fs,ts
    Const ForAppending = 8
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
    ts.WriteLine strText
    ts.Close
End Sub
'***********************************