Imports System
Module Program
Function LargestPrimeFactor(ByVal n As Long) As Long
Dim div As Long = 2
Dim maxPFact As Long = -1
While n <> 0
If n Mod div <> 0 Then
div += 1
Else
maxPFact = n
n \= div ' Integer division
If n = 1 Then Exit While
End If
End While
Return maxPFact
End Function
Sub Main()
Dim n As Integer = 124
Console.WriteLine(LargestPrimeFactor(n)) ' 2 x 2 x 31
Console.WriteLine(LargestPrimeFactor(288)) ' 2 x 2 x 2 x 2 x 2 x 3 x 3
Console.WriteLine(LargestPrimeFactor(1288)) ' 2 x 2 x 2 x 7 x 23
Console.WriteLine(LargestPrimeFactor(100000000)) ' 2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5, 5, 5
End Sub
End Module
' run:
'
' 31
' 3
' 23
' 5
'