Note that there are some explanatory texts on larger screens.

plurals
  1. POI have a Python list of the prime factors of a number. How do I (pythonically) find all the factors?
    primarykey
    data
    text
    <p>I'm working on a Project Euler problem which requires factorization of an integer. I can come up with a list of all of the primes that are the factor of a given number. The Fundamental Theorem of Arithmetic implies that I can use this list to derive <em>every</em> factor of the number.</p> <p>My current plan is to take each number in the list of base primes and raise its power until it is no longer an integer factor to find the maximum exponents for each prime. Then, I will multiply every possible combination of prime-exponent pairs. </p> <p>So for example, for 180:</p> <pre><code>Given: prime factors of 180: [2, 3, 5] Find maximum exponent of each factor: 180 / 2^1 = 90 180 / 2^2 = 45 180 / 2^3 = 22.5 - not an integer, so 2 is the maximum exponent of 2. 180 / 3^1 = 60 180 / 3^2 = 20 180 / 3^3 = 6.6 - not an integer, so 2 is the maximum exponent of 3. 180 / 5^1 = 36 180 / 5^2 = 7.2 - not an integer, so 1 is the maximum exponent of 5. </code></pre> <p>Next, do every combination of these up to the maximum exponent to get the factors:</p> <pre><code> 2^0 * 3^0 * 5^0 = 1 2^1 * 3^0 * 5^0 = 2 2^2 * 3^0 * 5^0 = 4 2^0 * 3^1 * 5^0 = 3 2^1 * 3^1 * 5^0 = 6 2^2 * 3^1 * 5^0 = 12 2^0 * 3^2 * 5^0 = 9 2^1 * 3^2 * 5^0 = 18 2^2 * 3^2 * 5^0 = 36 2^0 * 3^0 * 5^1 = 5 2^1 * 3^0 * 5^1 = 10 2^2 * 3^0 * 5^1 = 20 2^0 * 3^1 * 5^1 = 15 2^1 * 3^1 * 5^1 = 30 2^2 * 3^1 * 5^1 = 60 2^0 * 3^2 * 5^1 = 45 2^1 * 3^2 * 5^1 = 90 2^2 * 3^2 * 5^1 = 180 </code></pre> <p>So the list of factors = [1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 30, 36, 45, 60, 90, 180]</p> <p>Here is the code I have so far. Two problems: First, I don't think it is very Pythonic at all. I'd like to fix that. Second, I <em>really</em> don't have a Pythonic way to do the combinatoric second step. Out of shame, I've spared you from the ridiculous set of loops.</p> <p>n is the number we want to factor. listOfAllPrimes is a precalculated list of the primes up to 10 million.</p> <pre><code>def getListOfFactors(n, listOfAllPrimes): maxFactor = int(math.sqrt(n)) + 1 eligiblePrimes = filter(lambda x: x &lt;= maxFactor, listOfAllPrimes) listOfBasePrimes = filter(lambda x: n % x ==0, eligiblePrimes) listOfExponents = [] #(do I have to do this?) for x in listOfBasePrimes: y = 1 while (x**(y+1)) % n == 0: y += 1 listOfExponents.append(y) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload