Closed Thread
Results 1 to 11 of 11

Decomposition in Prime Factors

Hybrid View

  1. #1
    Registered User
    Join Date
    06-11-2011
    Location
    san francisco
    MS-Off Ver
    Excel 2007
    Posts
    9

    Decomposition in Prime Factors

    Hi,

    I am looking for a script to decompose a list of numbers into their prime factors.

    Attached is a file with an example of what I am looking for.

    Thanks for your help,

    Philippe
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: Decomposition in Prime Factors

    Google search on "decomposition in prime factors +excel" yielded more than 100 k answers -

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Decomposition in Prime Factors

    Function Factors(ByVal dNum As Double, _
                     Optional bRevOrder As Boolean = False) As Variant
        ' shg 2006-0923
        '     2006-0604 - added bRevOrder argument
        ' Returns a comma-delimited string of the factors of dNum
    
        Dim vFac        As Variant
        Dim asFac()     As String
        Dim iFac        As Long
        
        vFac = FirstFactor(dNum)
    
        Select Case VarType(vFac)
            Case vbError, vbString
                Factors = vFac
    
            Case vbBoolean
                Factors = dNum
                
            Case vbDouble
                ReDim asFac(1 To 50)    ' 2^50 > 1E+15
                
                Do Until dNum = 1
                    iFac = iFac + 1
                    asFac(iFac) = vFac
                    dNum = dNum / vFac
                    vFac = FirstFactor(dNum)
                Loop
                
                ReDim Preserve asFac(1 To iFac)
                If bRevOrder Then ArrRev asFac
                Factors = Join(asFac, "*")
        End Select
    End Function
    
    Private Function ArrRev(ByRef av As Variant)
        Dim i           As Long
        Dim j           As Long
        Dim vTmp        As Variant
    
        i = LBound(av)
        j = UBound(av)
    
        Do While i < j
            vTmp = av(i)
            av(i) = av(j)
            av(j) = vTmp
            i = i + 1
            j = j - 1
        Loop
    End Function
    
    Function FirstFactor(dNum As Double) As Variant
        ' shg 2006-0923
        '     2009-1003 fixed bug so if dNum Mod 15 = 0, returns 3 as a factor, not 5
    
        ' Returns:      if dNum is:
        '   #VALUE!       < 2
        '   "Too big!"    > 1E+15
        '   #VALUE!       <> Int(dNum)
        '   1st factor    otherwise
    
        Dim dQuo        As Double
        Dim dFac        As Double
    
        If Int(dNum) <> dNum Then
            FirstFactor = CVErr(xlErrValue)
    
        ElseIf dNum > 1E+15 Then
            FirstFactor = "Too big!"
    
        ElseIf Int(dNum / 2#) = dNum / 2 Then
            FirstFactor = 2#
        Else
            For dFac = 3# To Int(Sqr(dNum)) Step 2#
                dQuo = dNum / dFac
                If Int(dQuo) = dQuo Then
                    FirstFactor = dFac
                    Exit Function    '------------------------------------->
                End If
            Next dFac
    
            FirstFactor = dNum
        End If
    End Function
    E.g.,

          -A- ---B--- ------------C------------
      1    56 2*2*2*7 B1 and down: =Factors(A1)
      2    22 2*11                             
      3    36 2*2*3*3                          
      4    84 2*2*3*7                          
      5   236 2*2*59                           
      6   457 457                              
      7   826 2*7*59
    Last edited by shg; 07-03-2011 at 07:29 PM.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    06-11-2011
    Location
    san francisco
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Decomposition in Prime Factors

    Hi,

    I tried this code but got this error message: Argument not optional

    Any idea of what is wrong?

    Philippe

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Decomposition in Prime Factors

    "Tried it" in what way?

  6. #6
    Registered User
    Join Date
    06-11-2011
    Location
    san francisco
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Decomposition in Prime Factors

    I created a module with the code.

    Then pressed alt f8 and made the Factors function run.

    This is when I received the error message.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Decomposition in Prime Factors

    You used a formula on a worksheet, or wrote code to test it? If the latter, post the code.

  8. #8
    Registered User
    Join Date
    05-31-2012
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Decomposition in Prime Factors

    I clicked select code copied the code then opened a new module in the vb editor and pasted the code in. then selected "close and return to excel" on the file menu. I selected a column of values and pressed Alt+F8 to view macros the macro doesn't appear on the list. So I looked at the code, didn't find "sub Marco_name()" or "end sub" so I deleted that module created a new one and typed "sub prime_factors()" and Enter. Then I pasted the code in between exited to excel and tried to run the macro by selecting a range of values typing "Alt+F8" and got the error "expecting endsub" what am I doing wrong please

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Decomposition in Prime Factors

    Captain Pedro,

    Welcome to the Forum, unfortunately:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1