Feedback

Primfaktorzerlegung

Sprache: VB

Diese Funktion gibt eine Liste aller Primfaktoren von n mit oder ohne Exponenten an. Bsp.: [code]pr_fac(123456, True) -> {(2,6), (3,1), (643,1)} (List(Of String)) pr_fac(123456, False) -> {2,3,643} (List(Of Integer))[/code] Intern wird eine Funktion zur Ermittlung des kleinsten Teilers verwendet, welche Sie hier finden: http://dotnet-snippets.de/dns/kleinsten-teiler-ermitteln-SID1471.aspx
Function pr_fac(ByVal n As Integer, ByVal exp As Boolean) As IList
    If Not exp Then
        Dim facs As New List(Of Integer)

        While Not n = 1
            Dim p As Integer = sm_div(n)
            n = p

            While n Mod p = 0
                n = p
            End While
            facs.Add(p)
        End While

        Return facs
    Else
        Dim facs_exp As New List(Of String)

        While Not n = 1
            Dim p As Integer = sm_div(n), e As Byte = 1
            n = p

            While n Mod p = 0
                n = p
                e += 1
            End While
            facs_exp.Add("(" & p & "," & e & ")")
        End While

        Return facs_exp
    End If
End Function
Function pr_fac(ByVal n As Integer, ByVal exp As Boolean) As IList
    If Not exp Then
        Dim facs As New List(Of Integer)

        While Not n = 1
            Dim p As Integer = sm_div(n)
            n = p

            While n Mod p = 0
                n = p
            End While
            facs.Add(p)
        End While

        Return facs
    Else
        Dim facs_exp As New List(Of String)

        While Not n = 1
            Dim p As Integer = sm_div(n), e As Byte = 1
            n = p

            While n Mod p = 0
                n = p
                e += 1
            End While
            facs_exp.Add("(" & p & "," & e & ")")
        End While

        Return facs_exp
    End If
End Function