Deel een getal door allemaal priemgetallen. [vb.net]

Status
Niet open voor verdere reacties.

Mr Wolf

Gebruiker
Lid geworden
27 okt 2009
Berichten
107
Deel een getal door allemaal priemgetallen.

Voorbeeld:
vul 120 in
druk op bereken
En je ziet dat
2*2*2*3*5 = 120
2, 3 en 5 zijn priemgetallen.

Volgens mijn wiskundeboek wordt dit veel gebruikt bij internetbankieren.

Hieronder staat de vb.net code, en daaronder een zip met het programma er in.
Code:
Public Class Form1
    Private Function GetDivisionNumber(ByVal n As Integer) As Integer
        If n Mod 2 = 0 And n <> 2 Then
            GetDivisionNumber = 2
            Exit Function
        Else
            For i As Integer = 3 To n / 2 Step 2
                If n Mod i = 0 Then
                    GetDivisionNumber = i
                    Exit Function
                End If
            Next
        End If
        GetDivisionNumber = -1
    End Function

    Private Function PartByPrimes(ByVal n) As List(Of Integer)
_start:
        Dim rList As New List(Of Integer)
        Dim startIndex As Integer = 3
        If GetDivisionNumber(n) = -1 Then
            rList.Add(n)
            PartByPrimes = rList
            Exit Function
        End If

        If n Mod 2 = 0 Then
            Dim r As Integer = n / 2
            rList.Add(2)
            rList.AddRange(PartByPrimes(r))
            startIndex = 2
            GoTo _end
        End If

        For i As Integer = startIndex To n / 2 Step 2
            If n Mod i = 0 Then
                If GetDivisionNumber(i) = -1 Then
                    Dim r As Integer = n / i
                    rList.Add(i)
                    rList.AddRange(PartByPrimes(r))
                    startIndex = i
                    GoTo _end
                End If
            End If
        Next
_end:
        Dim cN As Integer
        For Each x As Integer In rList
            If cN = Nothing Then
                cN = x
            Else
                cN *= x
            End If
        Next
        If cN <> n Then
            If startIndex > n / 2 - 2 Then
                rList.Clear()
                PartByPrimes = rList
                Exit Function
            End If
            rList.Clear()
            GoTo _start
        End If
        PartByPrimes = rList
    End Function

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            ListBox1.Items.Clear()
            Dim dN As Integer = 1
            Dim l As List(Of Integer) = PartByPrimes(NumericUpDown1.Value)

            For Each x As Integer In l
                ListBox1.Items.Add(x.ToString + " * " + dN.ToString + " = " + (dN * x).ToString)
                dN *= x
            Next
        Catch ex As Exception
            ListBox1.Items.Clear()
            ListBox1.Items.Add("MISLUKT")
        End Try
    End Sub

    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        NumericUpDown1.Value = Math.Round(NumericUpDown1.Value, MidpointRounding.AwayFromZero)
    End Sub
End Class

Mr. Wolf
 

Bijlagen

Laatst bewerkt:
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan