Our List Factors code in Visual Basic.
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 = 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(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
Main Class:
Module Arithmetic_list_factors
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
Try it out!
Elegance (0.0)