usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

How to Check for Prime Numbers using Visual Basic



The Intrique of Prime Numbers | Detailed Explanation for VB.Net Kids

Prime numbers are tricky to spot.
A number that looks like a prime may in fact be a multiple of a smaller prime number.

In this math programming tutorial for kids, we explore how Visual Basic can be used to verify prime numbers efficiently.
So let's see how to know for sure that a number is a prime.
Of-course there is only one way:


Understanding Prime Number Logic in Visual Basic

A prime number is one that can only be divided by itself and one (1).

Let's try to draft a Visual Basic algorithm that checks prime numbers, with the number 97 in consideration.
To know for sure whether it is a prime number, we will recursively (repetitively) divide it by every number between 2 and 96 (97 minus 1).
If none of these divisors gives a remainder of zero, then 97 is certainly a prime number.


Create a new VB.Net class file; Project, Add Class.
Call it checkPrime.vb.
Optionally, Create a new VB.Net module file; Project, Add Module.
Call it CheckPrimeModule.vb.
Type out the adjoining VB.Net code for checking for primeness.


Base Theory of Quick-Check for Primeness in VB.Net

Since the world is always in a hurry, we can make use of a little extra speed.
This Visual Basic code example shows how to check if a number is prime using a fast algorithm based on complementary factors.

Consider the number 36; Its factors are:

1, 2, 3, 4, 6, 9, 12, 18 and 36.

Every factor of 36, when arranged in ascending or descending order, can be divided into 2 equal parts at the position of its square-root.

1, 2, 3, 4, |, 9, 12, 18, 36

It is easily seen that every factor of 36 on one side of the divide has a complementary factor on the other side.

Fast check for Prime numbers in VB.Net using complementary factors
Figure: Complementary factors to expedient quick check for prime numbers in VB.Net.

Hence, we can search for only a particular group of factors, (preferably the more compact group, i.e, between 1 and √36) to see if 36 has any factors.


A fast Check for Primeness in VB.Net

So for our quick prime number check VB.Net algorithm, we will use the range of 2 to √number.
Type out the adjoining VB.Net code for fast prime number check.


Note: You can simply tweak the existing function from the previous exercise.
You can comment out the VB.Net code from the main class from the previous lesson if you have been following.


So! Visual Basic Fun Practice Exercise - Check Prime Number

As a fun practice exercise, feel free to try out your own numbers, and see how the Visual Basic code checks the numbers to ascertain which ones are prime numbers.







VB.Net Code for Checking Prime - Class File.

Public Class CheckPrime

    Dim prime_suspect As Integer
    Dim a_factor As Integer

    ' Simulate a constructor
    Public Sub _init_(suspect As Integer)
        prime_suspect = suspect
    End Sub

    ' returns True If $prime_suspect Is a prime False otherwise.
    Public Function verifyPrime() As Boolean
        ' prime_suspect Is a prime number until proven otherwise
        ' Loop through searching For factors.
        For count = 2 To prime_suspect - 1
            If prime_suspect Mod count = 0 Then
                a_factor = count
                Return False
            End If
        Next count

        ' if no then factor is found:
        Return True
    End Function

    Public Property possible_factor() As Integer
        Get
            Return a_factor
        End Get
        Set(value As Integer)

        End Set
    End Property

End Class

VB.Net Code for Checking Prime - Main Class.

Module Arithmetic_CheckPrime

    Sub Main()
        ' Use the check prime Class
        Dim test_guy = 93
        Dim prime_check As New CheckPrime
        prime_check._init_(test_guy)

        Dim result = "Prime State:" & Environment.NewLine
        If prime_check.verifyPrime() Then
            result = test_guy & " is a prime number."
        Else
            result += test_guy & " is not a prime number." & Environment.NewLine
            result += "At least one factor of " & test_guy & " is " & prime_check.possible_factor
        End If

        Console.WriteLine(result)

    End Sub

End Module

VB.Net Code for Checking Prime Fast

Public Class CheckPrimeFast

    Dim prime_suspect As Integer
    Dim a_factor As Integer
    Dim test_range As Integer

    ' Simulate a constructor
    Public Sub _init_(suspect As Integer)
        prime_suspect = suspect
    End Sub

    ' returns True If $prime_suspect Is a prime False otherwise.
    Public Function verifyPrime() As Boolean
        ' prime_suspect Is a prime number until proven otherwise
        ' Loop through searching For factors.
        test_range = CInt(Math.Ceiling(Math.Sqrt(prime_suspect)))
        For count = 2 To test_range
            If prime_suspect Mod count = 0 Then
                a_factor = count
                Return False
            End If
        Next count

        ' if no then factor is found:
        Return True
    End Function

    Public Property possible_factor() As Integer
        Get
            Return a_factor
        End Get
        Set(value As Integer)

        End Set
    End Property

End Class

VB.Net Code for Checking Prime Fast - Main Module.

Module Arithmetic_CheckPrimeFast

    Sub Main()
        ' Use the check prime fast Class
        Dim test_guy = 98
        Dim prime_check As New CheckPrimeFast
        prime_check._init_(test_guy)

        Dim result = "Prime State:" & Environment.NewLine
        If prime_check.verifyPrime() Then
            result = test_guy & " is a prime number."
        Else
            result += test_guy & " is not a prime number." & Environment.NewLine
            result += "At least one factor of " & test_guy & " is " & prime_check.possible_factor
        End If

        Console.WriteLine(result)

    End Sub

End Module




<< PreviousNext >>