On 22/09/2026 11:28, Sophoklis Goumas wrote:
Hello,

I'd like to propose a new option for GNU Coreutils' factor utility.

Currently:

------------------------------------------------------------------------

$ factor 72
72: 2 2 2 3 3

------------------------------------------------------------------------

I would like to add an option, tentatively -a, --all,
that prints all positive divisors of the number in ascending order:

------------------------------------------------------------------------

$ factor -a 72
72: 1 2 3 4 6 8 9 12 18 24 36 72

------------------------------------------------------------------------

The implementation would reuse the existing prime factorization.

For a factorization:
n = p_{1}^e_{1} * p_{2}^e_{2} * ... * p_{k}^e_{k}

where the p_{i} are distinct primes,
the divisors can be generated by considering all choices of exponents:

p_{1}^a_{1} * p_{2}^a_{2} * ... * p_{k}^a_{k}
where 0 <= a_{i} <= e_{i}.

I would keep the existing factoring algorithms unchanged
and add the divisor enumeration as an output mode after factorization.

The implementation would handle both
the existing native integer path and the GNU MP arbitrary-precision path.

I am new to contributing to Coreutils, so before I start writing
a substantial patch, I would appreciate guidance or comments
on whether this functionality would be considered a useful addition
and whether -a/--all would be an appropriate interface.

If the idea is considered worthwhile, I would also appreciate any advice
on the preferred implementation structure and tests.
I am happy to prepare the patch and
test it against the current development sources.

Would someone be willing to mentor/review the initial implementation
and/or the overall patch submission?

Thanks in advance for your time and energy,
Sophoklis

Interesting.

This could be done outside of factor(1) of course,
and I played around with the attached script
to do that with factor(1) and bc(1), which works well/efficiently.
Note I used bc rather than awk to preserve factor's arbitrary precision.

Now that isn't trivial, so if this was a common use case
it may be worth adding the functionality to factor(1).
It does seem like a natural addition TBH.

Have you any practical use cases where this might be used?

thank you,
Padraig
#!/bin/sh

factor "$@" |
while read -r label factors
do
  printf '%s ' "$label"
  set -- $factors

  {
    printf '
define d(i,x) {
  auto j,y,z
  j=i
  while (j<%d && p[j]==p[i]) j=j+1
  y=x
  while (1) {
    if (j==%d) y else z=d(j,y)
    if (i==j) return(0)
    y=y*p[i]
    i=i+1
  }
}\n' "$#" "$#"

    i=0
    for p; do
      printf 'p[%d]=%s\n' "$i" "$p"
      i=$(($i + 1))
    done

    printf 'z=d(0,1)\n'
  } |
  BC_LINE_LENGTH=0 bc |
  sort -n |
  paste -sd ' '
done

Reply via email to