G
Guest
Is there a formula that I can use to find the prime factors of a given number?
Try this, assuming number is in A1:
=LARGE((ROUND($A$1/ROW(INDIRECT("$1:$"&$A$1)),0)=$A$1/ROW(INDIRECT("$1:$"&$A$1)))*ROW(INDIRECT("$1:$"&$A$1)),ROW(1:1))
Enter as an array formula - Ctl+Shift+Enter and drag/copy down as far
as necessary to show all factors. Works for positive numbers only.
Would need to put ABS($A$1) for all instances of $A$1 if there was a
danger the number could be negative.
Declan O'R
Grabbed this code from one of these news groups.
Wish I could attribute, but can't remember...
Perhaps a slight change to this excellent code might be:
For Fact = 2 To Sqr(OriginalNumber)
I was just messing around, and wrote a variation on this theme as follows:
The idea here was to skip 2 numbers at a time. (ie numbers ending in
1,3,5,7,9...)
Of course, checking 5 is a waste also, but it cuts the number of searches by
half.
Sub FactorInteger()
'// Needs: Microsoft Scripting Runtime
Dim Fact As Long
Dim TheRest As Long
Dim Limit As Long
Dim d As New Dictionary
TheRest = CLng(Val(InputBox("Number:")))
'// Just '2'
Do While TheRest / 2 = Int(TheRest / 2)
d.Add d.Count, 2
TheRest = TheRest / 2
Loop
Fact = 3
Limit = Sqr(TheRest)
Do Until TheRest = 1 Or Fact > Limit
If TheRest / Fact = Int(TheRest / Fact) Then
d.Add d.Count, Fact
TheRest = TheRest / Fact
Else
Fact = Fact + 2
End If
Loop
If TheRest > 1 Then d.Add d.Count, TheRest
[A:A].Clear
[A1].Resize(d.Count) = WorksheetFunction.Transpose(d.Items)
Cells(d.Count + 2, 1).FormulaR1C1 = "=Product(R1C:R[-2]C)"
End Sub
Looks like a good idea to limit variables to Long. Otherwise, using these
techniques, it may take a long time to factor a number
like 100000099999829 into 10000019 & 9999991.
HTH :>)
-- one of its prime factors is 5, but it is not returned by your
algorithm.
Ron Rosenfeld said:Perhaps a slight change to this excellent code might be:
For Fact = 2 To Sqr(OriginalNumber)
I was just messing around, and wrote a variation on this theme as follows:
The idea here was to skip 2 numbers at a time. (ie numbers ending in
1,3,5,7,9...)
Of course, checking 5 is a waste also, but it cuts the number of searches
by
half.
Sub FactorInteger()
'// Needs: Microsoft Scripting Runtime
Dim Fact As Long
Dim TheRest As Long
Dim Limit As Long
Dim d As New Dictionary
TheRest = CLng(Val(InputBox("Number:")))
'// Just '2'
Do While TheRest / 2 = Int(TheRest / 2)
d.Add d.Count, 2
TheRest = TheRest / 2
Loop
Fact = 3
Limit = Sqr(TheRest)
Do Until TheRest = 1 Or Fact > Limit
If TheRest / Fact = Int(TheRest / Fact) Then
d.Add d.Count, Fact
TheRest = TheRest / Fact
Else
Fact = Fact + 2
End If
Loop
If TheRest > 1 Then d.Add d.Count, TheRest
[A:A].Clear
[A1].Resize(d.Count) = WorksheetFunction.Transpose(d.Items)
Cells(d.Count + 2, 1).FormulaR1C1 = "=Product(R1C:R[-2]C)"
End Sub
Looks like a good idea to limit variables to Long. Otherwise, using these
techniques, it may take a long time to factor a number
like 100000099999829 into 10000019 & 9999991.
HTH :>)
Hmmm.
1234567890
-- one of its prime factors is 5, but it is not returned by your
algorithm.
--ron
Perhaps it was Harald Staff in .prog in early 2001 ? <g>,
re-this googled post: http://tinyurl.com/7f5qg
Hi Ron. If I input 1234567890, then the numbers I get for cells A1:A6 are:
2,3,3,5,3607, 3803.
I show one `5`. These factors check with another program. What numbers did
you get?
Ron Rosenfeld said:<sheepish look> I inputted the wrong number to be missing the '5'.
After rechecking, it turns out I was inputting 123456789 rather than
1234567890.
Of interest is that the prime factors for 123456789 are very similar.
Just eliminate the 2 and the 5.
That would be because putting a 0 on the end is the same as
multiplying by 10?
Alan.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.