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







<< PreviousNext >>

How to find all Factors of a Number in Visual Basic



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

Factors are the complete set of integers that will divide a particular numbers without remainder.
Take 36 as an example, it's complete set are:

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

Other than prime numbers, every other number has at least one factor - not considering 1 as factor.
Where there is just one factor, then this factor is the square root of the number in question;
In this guide, we'll explore the math behind factors-of-numbers and walk through how to code a Visual Basic algorithm for listing factors in a simple and fun way.


Code Logic for Factorising Numbers in Visual Basic - Fun Maths Exercise

Actually, we've been doing factors over the last two demonstrations (lessons).

We can implement a VB.Net algorithm for factorising a number by simply checking for our factors using the square-root of number range.
We'll start from 1 (which is always a factor).
For each found factor, we'll get the corresponding complementary factor by dividing the number (whose factors we are trying to find), by the found factor.
This Math activity and Visual Basic script help primary school students understand factorization by listing all factors of a number.


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

Type out the adjoining Visual Basic (VB.Net) code for listing the factors of any number.


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 Factors

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









VB.Net Code for List Factors - Class File.

Public Class ListFactors

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

    ' Simulate a constructor
    Public Sub _init_(candidate As Integer)
        find_my_factors = candidate
        found_factors.Add(1)
        found_factors.Add(find_my_factors) ' 1 and itself are automatic factors
        sqrt_range = CInt(Math.Ceiling(Math.Sqrt(find_my_factors)))
    End Sub

    ' Returns an array reference Of the desired factors
    Public Function findFactors() As List(Of Integer)
        ' Loop through 1 To 'find_my_factors' and test for divisibility.
        For count = 2 To sqrt_range
            If find_my_factors Mod count = 0 Then
                found_factors.Add(count)
                ' Get the complementing factor by dividing 'find_my_factor' by variable count.
                found_factors.Add(CInt(find_my_factors / count))
            End If
        Next count

        ' Sort the array in ascending order Not entirely necessary.
        found_factors.Sort()

        Return found_factors

    End Function

End Class

VB.Net Code for List Factors - Main Module.

Module Arithmetic_ListFactors

    Sub Main()

        ' Use the list factors Class
        Dim test_guy = 48
        Dim factor_list As New ListFactors
        factor_list._init_(test_guy)
        Dim answer = factor_list.findFactors()

        Console.WriteLine("Factors of " & test_guy & " include:")
        Console.WriteLine(String.Join(", ", answer))

    End Sub

End Module




<< PreviousNext >>