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







<< PreviousNext >>

Visual Basic Code to List Prime Pactors of a Number - Math Project for Primary Students



What are Prime Factors? | Maths Explanation for VB.Net Kids

Finding prime factors is all about selecting those factors of a number that are prime.
The prime factors of 36 as an example, are:

2 X 2 X 3 X 3.

In this guide, we'll thoroughly explain prime factorisation and show how to code a Visual Basic algorithm to find prime-factors in a detailed and interactive manner.
This Math exercise and Visual Basic algorithm will help young students understand prime factorization by listing all prime factors of a number.


Step-by-step Guide to Prime Factorisation of Numbers in Visual Basic

We'll go about the VB.Net algorithm to find prime factors in a simple way:

Step 1:

Starting with 2, find the first factor of the number of interest - this factor will definitely be a prime.
Store this factor away.

Step 2:

Divide number in question by found factor.

Step 3:

Using result from Step 2 as the new number in question (number whose prime factors we are trying to find), repeat Step 1.

Step 4:

Continue recursively until we arrive at 1.

Step 5:

We'll use the square-root of number range.


Create a new VB.Net class file; Project, Add Class.
Call it listPrimeFactors.vb.
Optionally, Create a new VB.Net module file; Project, Add Module.
Call it ListPrimeFactorsModule.vb.

Type out the adjoining Visual Basic (VB.Net) code for finding prime numbers.


Note: You can just reuse the VB.Net main module from the previous lesson if you have been following.


So! Visual Basic Fun Practice Exercise - List Prime Factors

As a fun practice exercise, feel free to try out your own different numbers, and see how the Visual Basic code lists the prime factors of those numbers.









VB.Net Code for List Prime Factors - Class File.

Public Class ListPrimeFactors

    Dim find_my_factors As Integer
    Dim found_prime_factors As New List(Of Integer)
    Dim sqrt_range As Integer

    Public Sub _init_(value As Integer)
        find_my_factors = value
    End Sub


    ' takes one argument, the candidate For factorisation
    ' returns an array reference Of the prime factors
    Public Function onlyPrimeFactors() As List(Of Integer)
        Dim temp_limit = Math.Ceiling(Math.Sqrt(find_my_factors))

        ' STEP 1:
        Dim i = 2
        Do While i <= temp_limit
            ' STEP 4:
            ' avoid an infinite Loop With the i != 1 check.
            If i <> 1 And find_my_factors Mod i = 0 Then
                found_prime_factors.Add(i)
                ' STEP 2:
                find_my_factors = find_my_factors / i
                ' STEP 3:
                Return onlyPrimeFactors()
            End If

            i += 1
        Loop

        found_prime_factors.Add(find_my_factors)
        Return found_prime_factors

    End Function

End Class

VB.Net Code for List Prime Factors - Main Module.

Module Arithmetic_PrimeFactors

    Sub Main()

        ' Use the list factors Class
        Dim test_guy = 48
        Dim prime_factors As New PrimeFactors
        prime_factors._init_(test_guy)
        Dim answer = prime_factors.onlyPrimeFactors()

        Console.WriteLine("Prime factorising " & test_guy & " gives:")
        Console.WriteLine(String.Join(" X ", answer))

    End Sub

End Module




<< PreviousNext >>