+ Reply to Thread
Results 1 to 7 of 7

Outputting factors and prime factors of number into one cell

Hybrid View

  1. #1
    Registered User
    Join Date
    07-17-2009
    Location
    Bristol England
    MS-Off Ver
    Excel 2007
    Posts
    46

    Outputting factors and prime factors of number into one cell

    Hi, I've been trying to produce the factors of a number into a single cell. I have found the one for outputting them into a column, but need a few thousand, so cant have it in this format. I need randbetween(1,100) in column A and the factors in B. So A1=12 B1=1,2,3,4,6,12. The commas don't necessarily need to be there.

    I also will need in the next step produce Prime factors in one single cell. Eg A1=12 B1=2, 2, 6

    Sorry if it's been asked before, but I've had a search and can't quite find what I need.

    Thanks in advance!
    Last edited by Geomarsh; 07-31-2009 at 07:42 AM. Reason: Extra question!

  2. #2
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,699

    Re: Outputting prime factors of number into one cell

    1,2,3,4,6 and 12 are all the factors, not the prime factors. Which one do you need?

  3. #3
    Registered User
    Join Date
    07-17-2009
    Location
    Bristol England
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: Outputting prime factors of number into one cell

    Sorry. I need all the factors.

    But thinking about it, I will need Prime factors also in the same format in the not too distant future so if you know a way to do either that would be great!

    Thanks
    Last edited by Geomarsh; 07-31-2009 at 07:31 AM.

  4. #4
    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: Outputting factors and prime factors of number into one cell

    Put this in a code module:
    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
    
        vFac = IsPrime(dNum, True)
    
        Select Case VarType(vFac)
            Case vbError, vbString
                Factors = vFac
    
            Case Else
                Do Until VarType(vFac) = vbBoolean
                    If bRevOrder Then
                        Factors = "," & CStr(vFac) & Factors
                    Else
                        Factors = Factors & CStr(vFac) & ","
                    End If
                    dNum = dNum / vFac
                    vFac = IsPrime(dNum, True)
                Loop
    
                If bRevOrder Then
                    Factors = CStr(dNum) & Factors
                Else
                    Factors = Factors & CStr(dNum)
                End If
    
        End Select
    End Function
    
    Function IsPrime(dNum As Double, Optional bFirstFactor As Boolean = False) As Variant
        ' shg 2006-0923
        '     2009-0603 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)
        '   FALSE         composite and bFirstFactor is False or omitted
        '   1st factor    composite and bFirstFactor is True
        '   TRUE          prime and <= 1E+15
    
        Dim dQuo    As Double
        Dim dFac    As Double
    
        If dNum < 2# Or Int(dNum) <> dNum Then
            IsPrime = CVErr(xlErrValue)
    
        ElseIf dNum = 2# Or dNum = 5# Then
            IsPrime = True
    
        ElseIf dNum > 1E+15 Then
            IsPrime = "Too big!"
    
        Else
            ' can't use Mod with numbers bigger than Longs, so ...
            Select Case Right(CStr(dNum), 1)
                Case "0", "2", "4", "6", "8"
                    IsPrime = IIf(bFirstFactor, 2#, False)
    
                Case "5"
                    If bFirstFactor Then
                        dQuo = dNum / 3#
                        IsPrime = IIf(Int(dQuo) = dQuo, 3#, 5#)
                    Else
                        IsPrime = False
                    End If
    
                Case Else
                    For dFac = 3# To Int(Sqr(dNum)) Step 2#
                        dQuo = dNum / dFac
                        If Int(dQuo) = dQuo Then
                            IsPrime = IIf(bFirstFactor, dFac, False)
                            Exit Function    '------------------------------------->
                        End If
                    Next dFac
    
                    IsPrime = True
            End Select
        End If
    End Function
    If A1 contains 123331060025819, then =Factors(A1) returns 113,12517,87195439
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    07-17-2009
    Location
    Bristol England
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: Outputting factors and prime factors of number into one cell

    Ok, Sorry but I'm a bit of a novice when it comes to this, but I have opened my macros screen thing, copied n pasted the code. Now what do I do? I used a bit of initiative and found the =isprime so typed that into the cell, but it just come out as 'true' or 'false' not with the actual factors.

    Thanks very much!
    Last edited by Geomarsh; 07-31-2009 at 10:47 AM.

  6. #6
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Outputting factors and prime factors of number into one cell

    Alt F11 is the shortcut. Then insert a module and paste it in.

    Nice code, shg!
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  7. #7
    Registered User
    Join Date
    07-17-2009
    Location
    Bristol England
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: Outputting factors and prime factors of number into one cell

    Ok found the right function, works great!!

    Any way to get all factors out not just Prime Factors?

    Also, is there any way to output these factors (prime and otherwise) into different cells in the same row?

    eg A1=12 B1=2 C1=2 D1=2 E1=3

    Im learning loads, Thanks!

+ Reply to 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