I have a list that represents a factorization of a number: its format is identical to the output of FactorInteger. I need to select the divisors that meet some criteria, but using Divisors seemed redundant because the number is already factored. Also, the number is extremely large, has several prime factors with 20+ digits, and has so many factors that generating the list of all divisors required so much memory and time. (My Mathematica crashed after taking around 13GB of memory)

For example, using $\frac{10^{180}-1}{9} = \underbrace{11...11}_{180 \space 1s}$ as a dummy number:

num = {{3, 2}, {7, 1}, {11, 1}, {13, 1}, {19, 1}, {31, 1}, {37, 1}, {41, 1},
 {61, 1}, {101, 1}, {181, 1}, {211, 1}, {241, 1}, {271, 1}, {2161, 1},
 {3541, 1}, {9091, 1}, {9901, 1}, {27961, 1}, {29611, 1}, {52579, 1},
 {238681, 1}, {333667, 1}, {2906161, 1}, {3762091, 1}, {4188901, 1},
 {39526741, 1}, {999999000001, 1}, {8985695684401, 1}, {4185502830133110721, 1},
 {4999437541453012143121, 1}, {1105097795002994798105101, 1}}

This is (the simplified version of) my code that crashed my computer:

Select[Divisors[Times @@ Power @@@ num], crit]

This meant that I can't simply make a list of all divisors and then use Select.

If the factors list did not contain any repeated prime factors, I could have done this:

factors = num[[All, 1]];
Reap[Do[If[crit, Sow[#]]&@ Inner[Power, factors,
  IntegerDigits[a, 2, Length[factors]], Times],
 {a, 0, 2^Length[factors] - 1)]]

This works because a is converted to binary, and each factor of the number is raised to a digit of a (1 or 0) and multiplied. As a increases, it will cover all possible divisors.

This cannot be done because my num has repeated prime factors. How could I do this with a large number with repeated prime factors?


The problem is that the list of all divisors is too large to store. I am looking for a way to generate and test criteria to divisors one by one so that the list of all divisors do not need to get stored during the process (see code that begins with factors). By divisors, I mean all numbers that can divide another number. For instance, divisors of 30 would be {1,2,3,5,6,10,15,30}.

  • 1
    $\begingroup$ It seems to me that f /@ DeleteDuplicates[num[[All, 1]]] should work. Am I missing something? $\endgroup$
    – m_goldberg
    Commented Jan 1, 2016 at 22:40
  • $\begingroup$ I am not sure where are you going with this because the occurence of repeated prime factors is part of the factorized representation of the original integer therefore, to eliminate the repeated occurrences of prime factors is in fact, changing the original integer. So I am not sure where is this going. I can tell you that for these type of performace issues there is ParallelTable which could avoid session crashes by distributing the computation among different kernels and cores in one or more machines. $\endgroup$ Commented Jan 2, 2016 at 0:51
  • $\begingroup$ @m_goldberg I need to apply f to all divisors of the number, not just the prime factors. num is a prime factorization, not a list of divisors. $\endgroup$ Commented Jan 2, 2016 at 2:34
  • 1
    $\begingroup$ @Schopenhauer I mentioned that the repeated prime factor makes the third code (begins with factors) unusable. I am looking for a way to apply f to all divisors without generating the full list of divisors. The code I presented successfully does the job but only works for numbers that have no repeated prime factors. I am trying to find a way to write a code that does the same task but does not fail for numbers with repeated prime factors. $\endgroup$ Commented Jan 2, 2016 at 2:38
  • $\begingroup$ @JHM So the question is more about controlling evaluation. I would not use the front end to carry out this task. I would use a script with the set of Parallel and Wait functions and put my partial results in files.mx or files.txt then use Get, ReadList or other method to scan these for the elements of interest to go further. I would also try a more general approach i.e. using Algebra. Maybe a little far fetch but these stream processing type of computations seem better suited for a GPU. $\endgroup$ Commented Jan 6, 2016 at 11:51

2 Answers 2


Here's a sketch of a way to do this:


(* fake some biggish number and get prime factorization *)    
num = Times @@ (Range[12]!)
fi = FactorInteger[num];

(* spit out divisors *)
ki = fi[[All, 2]] + 1;
vars = Transpose[{Unique[] & /@ ki, ki}];
result = Reap[
   With[{seq = Sequence @@ vars, vars = vars[[All, 1]]}, 
    Do[Sow[Times @@ (fi[[All, 1]]^(vars - 1))], seq]]];

(* check against Divisors fn. *)
Divisors[num] == Sort[result[[2, 1]]]




Simply sets up iterators based on multiplicity of prime factors, spits out all the combinations. Obviously, you'd do your whatever within the Do - I just Sow/Reap here to get them all for the small example.

N.B.: The results are unsorted, not sure there's an efficient way to produce a huge set sequentially in order, but the results are the distinct divisors in any case.

  • $\begingroup$ I have attempted this approach, using a Do loop, but the time taken was significantly longer. For a relatively small number (with 2 million divisors), f /@ Divisors[ ... ] took less than 1/8 of time taken by my Do loop code. My Do loop code is almost identical to this code. $\endgroup$ Commented Jan 2, 2016 at 7:45

There are 6,442,450,944 divisors. Are you sure you want to go there?

Anyway, you can just use Outer. Here is an example on the first seven primes in your factorization, yielding 192 unique divisors (including 1 and the number itself):

f = {{3, 2}, {7, 1}, {11, 1}, {13, 1}, {19, 1}, {31, 1}, {37, 1}};
Flatten[Outer[Times, ##] & @@ (#1^Range[0, #2] & @@@ f)]//Sort

That is the same as the output of:


You can trivially apply a function to each as they are generated (this time didn't bother sorting them):

Flatten[Outer[g[Times[##]] &, ##] & @@ (#1^Range[0, #2] & @@@ f)]

This fixes the what you "could have done", but that's still going to generate a giant list of six billion things. You need to say what you're going to do with the six billion function results.

Given the edited question, you can generate the divisors recursively and use Reap and Sow to return a (hopefully) small subset of them that meet a test. For example, applying the test g to the divisors generated by the factor list f (n starts as 1):

gen[{n_, f_}, g_] := 
 If[Length@f == 0, If[g@n, Sow@n];, 
  gen[{n f[[1, 1]]^#, Drop[f, 1]}, g] & /@ Range[0, f[[1, 2]]];]


f = {{3, 2}, {7, 1}, {11, 1}, {13, 1}, {19, 1}, {31, 1}, {37, 1}};
Reap[gen[{1, f}, # > 1000000 &];][[2, 1]]

gives all of the divisors greater than a million:


The recursion depth will be the length of the factors list, and so will take very little memory. The memory will be driven by how many divisors meet the criterion to Sow.

  • $\begingroup$ The problem is that generating the list of divisors would take too much time and memory. I am trying to find a way to apply a function f to each divisor without generating the entire list of divisors; I am not looking for a way to generate the list of divisors without Divisors. $\endgroup$ Commented Jan 2, 2016 at 7:16
  • $\begingroup$ That was not clear until your later "Edit:". What you said in your question before that edit was that you were trying to avoid the FactorInteger part of Divisors. Furthermore, your example of what you "could have done" does exactly this, generating all the values. I answered your original question: This cannot be done because my num has repeated prime factors. How could I apply a function to all divisors of a large number with repeated prime factors? $\endgroup$
    – Mark Adler
    Commented Jan 2, 2016 at 7:54
  • $\begingroup$ I realized that the entire question was misstated. The function I am using contains If and Sow, and I am trying to select the divisors that fit some criteria. Generating the entire list of divisors and then using Select would consume too much memory, so I was searching a way to select the desired divisors without doing that. $\endgroup$ Commented Jan 2, 2016 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.