Question

I am making an app in visual basic with .NET Framework 4. I have to generate a list of prime numbers as per the user's input. So far, for my output if you put in 5 for the first five prime numbers you get 3 5 7 7 9 11 11. I am not sure if my number increment is in the wrong place. Thanks for any help you can give me. Also, I'm not sure how to include 2 as a prime number in my code.

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
Was it helpful?

Solution

You should not use the else branch in the for loop, in this case, each time the Mod unequal to 0, you will touch the else block, take number 11 for example:

11 mod 2 <> 0, you went into the else block,

11 mod 3 <> 0, you went into the else block again!

you can divide the number by all numbers between 2 to sqrt(number), and then use isprime to check whether it is a prime like this.(I am using VBScript here)

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

and don't forget to truncate the square root of number.

OTHER TIPS

Just to share with you this script :

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
'***********************************
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top