dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: prime factors

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    prime factors

    [Originally posted by drop]

    how can I get the prime factors of a number?

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:prime factors

    [Originally posted by Mikekay2]

    The following code shows an easy to follow method:

    Dim Factors() As Long
    Dim Pointer As Long

    Private Sub Command1_Click()
    Dim I as Integer

    Pointer=0
    For I = LBound(Factors) To UBound(Factors)
    Factors(I) = 0
    Next

    ' set number to be the number you want to factorise
    While Not GetFactors(Number)
    Wend

    ' pointer -1 gives no of factors
    For I = 1 To Pointer - 1

    'Factors(i) will give each prime factor in turn
    Next
    End Sub

    Public Function GetFactors(ByRef ToFactorise As Long) As Boolean
    Dim I As Long

    For I = 2 To ToFactorise
    If ToFactorise Mod I = 0 Then
    If IsPrime(I) Then
    Factors(Pointer) = I
    Pointer = Pointer + 1
    ToFactorise = ToFactorise / I
    GetFactors = False
    Exit Function
    End If

    End If
    Next

    GetFactors = True



    End Function

    Public Function IsPrime(NumberToCheck As Long) As Boolean
    Dim I As Long

    For I = 2 To NumberToCheck / 2
    If NumberToCheck Mod I = 0 Then
    IsPrime = False
    Exit Function
    End If
    Next

    IsPrime = True

    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center