Repeat this GCD operation

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












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 < k$ such that $a_j$ does not divide $a_k$, and replace $a_j$ and $a_k$ by $gcd(a_j, a_k)$ and $textlcm(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] => [1, 2, 4, 8, 16, 32]
[120, 24, 6, 2, 1, 1] => [1, 1, 2, 6, 24, 120]
[97, 41, 48, 12, 98, 68] => [1, 1, 2, 4, 12, 159016368]
[225, 36, 30, 1125, 36, 18, 180] => [3, 9, 18, 90, 180, 900, 4500]
[17, 17, 17, 17] => [17, 17, 17, 17]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] => [1, 1, 1, 1, 1, 2, 2, 6, 60, 2520]









share|improve this question



























    up vote
    4
    down vote

    favorite












    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 < k$ such that $a_j$ does not divide $a_k$, and replace $a_j$ and $a_k$ by $gcd(a_j, a_k)$ and $textlcm(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] => [1, 2, 4, 8, 16, 32]
    [120, 24, 6, 2, 1, 1] => [1, 1, 2, 6, 24, 120]
    [97, 41, 48, 12, 98, 68] => [1, 1, 2, 4, 12, 159016368]
    [225, 36, 30, 1125, 36, 18, 180] => [3, 9, 18, 90, 180, 900, 4500]
    [17, 17, 17, 17] => [17, 17, 17, 17]
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] => [1, 1, 1, 1, 1, 2, 2, 6, 60, 2520]









    share|improve this question

























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      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 < k$ such that $a_j$ does not divide $a_k$, and replace $a_j$ and $a_k$ by $gcd(a_j, a_k)$ and $textlcm(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] => [1, 2, 4, 8, 16, 32]
      [120, 24, 6, 2, 1, 1] => [1, 1, 2, 6, 24, 120]
      [97, 41, 48, 12, 98, 68] => [1, 1, 2, 4, 12, 159016368]
      [225, 36, 30, 1125, 36, 18, 180] => [3, 9, 18, 90, 180, 900, 4500]
      [17, 17, 17, 17] => [17, 17, 17, 17]
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] => [1, 1, 1, 1, 1, 2, 2, 6, 60, 2520]









      share|improve this question















      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 < k$ such that $a_j$ does not divide $a_k$, and replace $a_j$ and $a_k$ by $gcd(a_j, a_k)$ and $textlcm(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] => [1, 2, 4, 8, 16, 32]
      [120, 24, 6, 2, 1, 1] => [1, 1, 2, 6, 24, 120]
      [97, 41, 48, 12, 98, 68] => [1, 1, 2, 4, 12, 159016368]
      [225, 36, 30, 1125, 36, 18, 180] => [3, 9, 18, 90, 180, 900, 4500]
      [17, 17, 17, 17] => [17, 17, 17, 17]
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] => [1, 1, 1, 1, 1, 2, 2, 6, 60, 2520]






      code-golf array-manipulation number-theory






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 46 mins ago

























      asked 55 mins ago









      Misha Lavrov

      3,696321




      3,696321




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote














          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.





          share|improve this answer





























            up vote
            0
            down vote














            Retina, 65 bytes



            d+
            *
            +`b((_+)(2)+)b(.*)b(?!1+b)(2+)b
            $2$4$5$#3*$5
            _+
            $.&


            Try it online! Link includes the faster test cases. Explanation:



            d+
            *


            Convert to unary.



            +`b((_+)(2)+)b(.*)b(?!1+b)(2+)b


            Repeatedly match: any number with a factor, then a later number that is not divisible by the first number but is divisible by the factor.



            $2$4$5$#3*$5


            $1 is the first number. $2 is the factor. Because regex is greedy this is the largest factor i.e the gcd. $4 is the part of the match between the original numbers. $5 is the second number. $#3 (in decimal rather than unary) is one less than $1 divided by $2, since it doesn't include the original $2. This means that to calculate the lcm we need to multiply $5 by one more than $#3 which is most succintly written as the sum of $5 and the product of $#3 and $5.



            _+
            $.&


            Convert to decimal.





            share




















              Your Answer





              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "200"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175090%2frepeat-this-gcd-operation%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              5
              down vote














              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.





              share|improve this answer


























                up vote
                5
                down vote














                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.





                share|improve this answer
























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote










                  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.





                  share|improve this answer















                  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.






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 16 mins ago

























                  answered 26 mins ago









                  Dennis♦

                  183k32293725




                  183k32293725




















                      up vote
                      0
                      down vote














                      Retina, 65 bytes



                      d+
                      *
                      +`b((_+)(2)+)b(.*)b(?!1+b)(2+)b
                      $2$4$5$#3*$5
                      _+
                      $.&


                      Try it online! Link includes the faster test cases. Explanation:



                      d+
                      *


                      Convert to unary.



                      +`b((_+)(2)+)b(.*)b(?!1+b)(2+)b


                      Repeatedly match: any number with a factor, then a later number that is not divisible by the first number but is divisible by the factor.



                      $2$4$5$#3*$5


                      $1 is the first number. $2 is the factor. Because regex is greedy this is the largest factor i.e the gcd. $4 is the part of the match between the original numbers. $5 is the second number. $#3 (in decimal rather than unary) is one less than $1 divided by $2, since it doesn't include the original $2. This means that to calculate the lcm we need to multiply $5 by one more than $#3 which is most succintly written as the sum of $5 and the product of $#3 and $5.



                      _+
                      $.&


                      Convert to decimal.





                      share
























                        up vote
                        0
                        down vote














                        Retina, 65 bytes



                        d+
                        *
                        +`b((_+)(2)+)b(.*)b(?!1+b)(2+)b
                        $2$4$5$#3*$5
                        _+
                        $.&


                        Try it online! Link includes the faster test cases. Explanation:



                        d+
                        *


                        Convert to unary.



                        +`b((_+)(2)+)b(.*)b(?!1+b)(2+)b


                        Repeatedly match: any number with a factor, then a later number that is not divisible by the first number but is divisible by the factor.



                        $2$4$5$#3*$5


                        $1 is the first number. $2 is the factor. Because regex is greedy this is the largest factor i.e the gcd. $4 is the part of the match between the original numbers. $5 is the second number. $#3 (in decimal rather than unary) is one less than $1 divided by $2, since it doesn't include the original $2. This means that to calculate the lcm we need to multiply $5 by one more than $#3 which is most succintly written as the sum of $5 and the product of $#3 and $5.



                        _+
                        $.&


                        Convert to decimal.





                        share






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote










                          Retina, 65 bytes



                          d+
                          *
                          +`b((_+)(2)+)b(.*)b(?!1+b)(2+)b
                          $2$4$5$#3*$5
                          _+
                          $.&


                          Try it online! Link includes the faster test cases. Explanation:



                          d+
                          *


                          Convert to unary.



                          +`b((_+)(2)+)b(.*)b(?!1+b)(2+)b


                          Repeatedly match: any number with a factor, then a later number that is not divisible by the first number but is divisible by the factor.



                          $2$4$5$#3*$5


                          $1 is the first number. $2 is the factor. Because regex is greedy this is the largest factor i.e the gcd. $4 is the part of the match between the original numbers. $5 is the second number. $#3 (in decimal rather than unary) is one less than $1 divided by $2, since it doesn't include the original $2. This means that to calculate the lcm we need to multiply $5 by one more than $#3 which is most succintly written as the sum of $5 and the product of $#3 and $5.



                          _+
                          $.&


                          Convert to decimal.





                          share













                          Retina, 65 bytes



                          d+
                          *
                          +`b((_+)(2)+)b(.*)b(?!1+b)(2+)b
                          $2$4$5$#3*$5
                          _+
                          $.&


                          Try it online! Link includes the faster test cases. Explanation:



                          d+
                          *


                          Convert to unary.



                          +`b((_+)(2)+)b(.*)b(?!1+b)(2+)b


                          Repeatedly match: any number with a factor, then a later number that is not divisible by the first number but is divisible by the factor.



                          $2$4$5$#3*$5


                          $1 is the first number. $2 is the factor. Because regex is greedy this is the largest factor i.e the gcd. $4 is the part of the match between the original numbers. $5 is the second number. $#3 (in decimal rather than unary) is one less than $1 divided by $2, since it doesn't include the original $2. This means that to calculate the lcm we need to multiply $5 by one more than $#3 which is most succintly written as the sum of $5 and the product of $#3 and $5.



                          _+
                          $.&


                          Convert to decimal.






                          share











                          share


                          share










                          answered 4 mins ago









                          Neil

                          77.2k744174




                          77.2k744174



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175090%2frepeat-this-gcd-operation%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?