Repeat this GCD operation Problem A3 from the 2008 Putnam competition says:


  Start with a finite sequence \$a_1, a_2, \dots, a_n\$ of positive integers. If possible, choose two indices \$j &lt; k\$ such that \$a_j\$ does not divide \$a_k\$, and replace \$a_j\$ and \$a_k\$ by \$\gcd(a_j, a_k)\$ and \$\text{lcm}(a_j, a_k)\$, respectively. Prove that if this process is repeated, it must eventually stop and the final sequence does not depend on the choices made.


Your goal in this challenge is to take a finite sequence of positive integers as input, and output the result of repeating this process until no further progress is possible. (That is, until every number in the resulting sequence divides all the numbers that come after it.) You don't need to solve the Putnam problem.

This is code-golf: the shortest solution in every programming language wins.

Test cases

[1, 2, 4, 8, 16, 32] =&gt; [1, 2, 4, 8, 16, 32]
[120, 24, 6, 2, 1, 1] =&gt; [1, 1, 2, 6, 24, 120]
[97, 41, 48, 12, 98, 68] =&gt; [1, 1, 2, 4, 12, 159016368]
[225, 36, 30, 1125, 36, 18, 180] =&gt; [3, 9, 18, 90, 180, 900, 4500]
[17, 17, 17, 17] =&gt; [17, 17, 17, 17]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] =&gt; [1, 1, 1, 1, 1, 2, 2, 6, 60, 2520]
Jelly, 9 bytes

ÆEz0Ṣ€ZÆẸ


Try it online!

How it works

ÆEz0Ṣ€ZÆẸ  Main link. Argument: A (array)

ÆE         For each n in A, compute the exponents of n's prime factorization.
           E.g., 2000000 = 2⁷3⁰5⁶ gets mapped to [7, 0, 6].
  z0       Zip 0; append 0's to shorter exponent arrays to pad them to the same
           length, then read the resulting matrix by columns.
    Ṣ€     Sort the resulting arrays (exponents that correspond to the same prime).
      Z    Zip; read the resulting matrix by columns, re-grouping the exponents by
           the integers they represent.
       ÆẸ  Unexponents; map the exponent arrays back to integers.