Computing only Prime Powers with NestList












4












$begingroup$


Is there a simple way to compute only prime powers of a function f with NestList? That is, I want to compute:



{f[x_0], f^2[x_0]=f[f[x_0]], f^3[x_0], f^5[x_0]...}


up to some specified prime power. Thanks so much!










share|improve this question









$endgroup$

















    4












    $begingroup$


    Is there a simple way to compute only prime powers of a function f with NestList? That is, I want to compute:



    {f[x_0], f^2[x_0]=f[f[x_0]], f^3[x_0], f^5[x_0]...}


    up to some specified prime power. Thanks so much!










    share|improve this question









    $endgroup$















      4












      4








      4





      $begingroup$


      Is there a simple way to compute only prime powers of a function f with NestList? That is, I want to compute:



      {f[x_0], f^2[x_0]=f[f[x_0]], f^3[x_0], f^5[x_0]...}


      up to some specified prime power. Thanks so much!










      share|improve this question









      $endgroup$




      Is there a simple way to compute only prime powers of a function f with NestList? That is, I want to compute:



      {f[x_0], f^2[x_0]=f[f[x_0]], f^3[x_0], f^5[x_0]...}


      up to some specified prime power. Thanks so much!







      recursion






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 9 hours ago









      user413587user413587

      775




      775






















          7 Answers
          7






          active

          oldest

          votes


















          3












          $begingroup$

          Lets say you have f(x) and you want results up to 10



          n=10;    
          NestList[f,x,n][[#+1]]&/@Prime[Range@PrimePi@n]



          {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}







          share|improve this answer











          $endgroup$





















            3












            $begingroup$

            Here's a way using Compose:



            n = 10; ComposeList[ConstantArray[f, n], f[x]][[#]] & /@ Prime[Range@PrimePi@n]

            {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}





            share|improve this answer









            $endgroup$





















              3












              $begingroup$

              Here's a way to accumulate the nestings, i.e. use the previous nesting as the starting point for the next one:



              primeNest[f_, x_, n_] := FoldList[Nest[f, ##] &, f[f[x]], Differences[Prime[Range[n]]]]

              primeNest[f, x, 5]



              {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], 
              f[f[f[f[f[f[f[x]]]]]]], f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}



              (Depth /@ primeNest[f, x, 20]) - 1



              {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}






              share|improve this answer









              $endgroup$





















                2












                $begingroup$

                One can in fact use NestWhile along with NestList, in the same spirit as this previous answer:



                NestList[NestWhile[f, f[#], ! PrimeQ[Depth[#] - 1] &] &, x, 5]
                {x, f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]],
                f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}


                Here is another variation which yields the same result:



                NestList[Nest[f, #, NextPrime[Depth[#] - 1] - Depth[#] + 1] &, x, 5]





                share|improve this answer









                $endgroup$





















                  1












                  $begingroup$

                  ClearAll[primesNest0]
                  primesNest0[f_, x_, n_] := Nest[f, x, #] & /@ Prime[Range@PrimePi@n]
                  primesNest0[f, x, 10]



                  {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                  Also



                  ClearAll[primesNest1]
                  primesNest1[f_, x_, n_] := Fold[#2@# &, x, ConstantArray[f, #]] & /@ Prime[Range@PrimePi@n]

                  primesNest1[f, x, 10]



                  {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                  and



                  ClearAll[primesNest2]
                  primesNest2[f_, x_, n_] := Compose[##&@@ConstantArray[f, #], x] & /@ Prime[Range@PrimePi@n]
                  primesNest2[f, x, 10]



                  {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                  and



                  ClearAll[primesNest3]
                  primesNest3[f_, x_, n_] := Composition[## & @@ ConstantArray[f, #]][x] & /@
                  Prime[Range@PrimePi@n]
                  primesNest3[f, x, 10]


                  . {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}






                  share|improve this answer











                  $endgroup$





















                    0












                    $begingroup$

                    This may not be an elegant and smart way to do it but here is one way.



                      exp = NestList[g, x, 12];
                    selector = PrimeQ[LeafCount /@ exp - 1];
                    Pick[exp, selector] /. {g -> f, x -> x0}



                    {f[f[x0]], f[f[f[x0]]], f[f[f[f[f[x0]]]]], f[f[f[f[f[f[f[x0]]]]]]],
                    f[f[f[f[f[f[f[f[f[f[f[x0]]]]]]]]]]]}







                    share|improve this answer











                    $endgroup$





















                      0












                      $begingroup$

                      Well, maybe not as elegant as with NestList, but for the first five primes (n=5), for example, here is another way



                      n = 5;
                      ls = {};
                      For [k = 0, k < n, k++,
                      (
                      Q = f[x];
                      For [j = 1, j < Prime[k + 1], j++,
                      Q = Map[f, Q];
                      ];
                      ls = Join[ls, {Q}]
                      )
                      ]
                      Print[ls]


                      Here is the result:



                      {f[f[x]],f[f[f[x]]],f[f[f[f[f[x]]]]],f[f[f[f[f[f[f[x]]]]]]],f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}





                      share|improve this answer










                      New contributor




                      mjw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






                      $endgroup$









                      • 1




                        $begingroup$
                        Why should I avoid the For loop in Mathematica?
                        $endgroup$
                        – corey979
                        8 hours ago










                      • $begingroup$
                        Yes, I've seen that! And I've got two loops! If n>10^4, great point! But for n=5 or n=10, do we care?, this code ran pretty quickly.
                        $endgroup$
                        – mjw
                        7 hours ago












                      • $begingroup$
                        Thank you for the tip, here it is with For replaced by Do: n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
                        $endgroup$
                        – mjw
                        7 hours ago













                      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.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "387"
                      };
                      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',
                      autoActivateHeartbeat: false,
                      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%2fmathematica.stackexchange.com%2fquestions%2f191716%2fcomputing-only-prime-powers-with-nestlist%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      7 Answers
                      7






                      active

                      oldest

                      votes








                      7 Answers
                      7






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      3












                      $begingroup$

                      Lets say you have f(x) and you want results up to 10



                      n=10;    
                      NestList[f,x,n][[#+1]]&/@Prime[Range@PrimePi@n]



                      {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}







                      share|improve this answer











                      $endgroup$


















                        3












                        $begingroup$

                        Lets say you have f(x) and you want results up to 10



                        n=10;    
                        NestList[f,x,n][[#+1]]&/@Prime[Range@PrimePi@n]



                        {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}







                        share|improve this answer











                        $endgroup$
















                          3












                          3








                          3





                          $begingroup$

                          Lets say you have f(x) and you want results up to 10



                          n=10;    
                          NestList[f,x,n][[#+1]]&/@Prime[Range@PrimePi@n]



                          {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}







                          share|improve this answer











                          $endgroup$



                          Lets say you have f(x) and you want results up to 10



                          n=10;    
                          NestList[f,x,n][[#+1]]&/@Prime[Range@PrimePi@n]



                          {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}








                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 8 hours ago

























                          answered 8 hours ago









                          J42161217J42161217

                          3,787321




                          3,787321























                              3












                              $begingroup$

                              Here's a way using Compose:



                              n = 10; ComposeList[ConstantArray[f, n], f[x]][[#]] & /@ Prime[Range@PrimePi@n]

                              {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}





                              share|improve this answer









                              $endgroup$


















                                3












                                $begingroup$

                                Here's a way using Compose:



                                n = 10; ComposeList[ConstantArray[f, n], f[x]][[#]] & /@ Prime[Range@PrimePi@n]

                                {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}





                                share|improve this answer









                                $endgroup$
















                                  3












                                  3








                                  3





                                  $begingroup$

                                  Here's a way using Compose:



                                  n = 10; ComposeList[ConstantArray[f, n], f[x]][[#]] & /@ Prime[Range@PrimePi@n]

                                  {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}





                                  share|improve this answer









                                  $endgroup$



                                  Here's a way using Compose:



                                  n = 10; ComposeList[ConstantArray[f, n], f[x]][[#]] & /@ Prime[Range@PrimePi@n]

                                  {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered 6 hours ago









                                  bill sbill s

                                  53.4k376152




                                  53.4k376152























                                      3












                                      $begingroup$

                                      Here's a way to accumulate the nestings, i.e. use the previous nesting as the starting point for the next one:



                                      primeNest[f_, x_, n_] := FoldList[Nest[f, ##] &, f[f[x]], Differences[Prime[Range[n]]]]

                                      primeNest[f, x, 5]



                                      {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], 
                                      f[f[f[f[f[f[f[x]]]]]]], f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}



                                      (Depth /@ primeNest[f, x, 20]) - 1



                                      {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}






                                      share|improve this answer









                                      $endgroup$


















                                        3












                                        $begingroup$

                                        Here's a way to accumulate the nestings, i.e. use the previous nesting as the starting point for the next one:



                                        primeNest[f_, x_, n_] := FoldList[Nest[f, ##] &, f[f[x]], Differences[Prime[Range[n]]]]

                                        primeNest[f, x, 5]



                                        {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], 
                                        f[f[f[f[f[f[f[x]]]]]]], f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}



                                        (Depth /@ primeNest[f, x, 20]) - 1



                                        {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}






                                        share|improve this answer









                                        $endgroup$
















                                          3












                                          3








                                          3





                                          $begingroup$

                                          Here's a way to accumulate the nestings, i.e. use the previous nesting as the starting point for the next one:



                                          primeNest[f_, x_, n_] := FoldList[Nest[f, ##] &, f[f[x]], Differences[Prime[Range[n]]]]

                                          primeNest[f, x, 5]



                                          {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], 
                                          f[f[f[f[f[f[f[x]]]]]]], f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}



                                          (Depth /@ primeNest[f, x, 20]) - 1



                                          {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}






                                          share|improve this answer









                                          $endgroup$



                                          Here's a way to accumulate the nestings, i.e. use the previous nesting as the starting point for the next one:



                                          primeNest[f_, x_, n_] := FoldList[Nest[f, ##] &, f[f[x]], Differences[Prime[Range[n]]]]

                                          primeNest[f, x, 5]



                                          {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], 
                                          f[f[f[f[f[f[f[x]]]]]]], f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}



                                          (Depth /@ primeNest[f, x, 20]) - 1



                                          {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered 6 hours ago









                                          Chip HurstChip Hurst

                                          21.3k15790




                                          21.3k15790























                                              2












                                              $begingroup$

                                              One can in fact use NestWhile along with NestList, in the same spirit as this previous answer:



                                              NestList[NestWhile[f, f[#], ! PrimeQ[Depth[#] - 1] &] &, x, 5]
                                              {x, f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]],
                                              f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}


                                              Here is another variation which yields the same result:



                                              NestList[Nest[f, #, NextPrime[Depth[#] - 1] - Depth[#] + 1] &, x, 5]





                                              share|improve this answer









                                              $endgroup$


















                                                2












                                                $begingroup$

                                                One can in fact use NestWhile along with NestList, in the same spirit as this previous answer:



                                                NestList[NestWhile[f, f[#], ! PrimeQ[Depth[#] - 1] &] &, x, 5]
                                                {x, f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]],
                                                f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}


                                                Here is another variation which yields the same result:



                                                NestList[Nest[f, #, NextPrime[Depth[#] - 1] - Depth[#] + 1] &, x, 5]





                                                share|improve this answer









                                                $endgroup$
















                                                  2












                                                  2








                                                  2





                                                  $begingroup$

                                                  One can in fact use NestWhile along with NestList, in the same spirit as this previous answer:



                                                  NestList[NestWhile[f, f[#], ! PrimeQ[Depth[#] - 1] &] &, x, 5]
                                                  {x, f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]],
                                                  f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}


                                                  Here is another variation which yields the same result:



                                                  NestList[Nest[f, #, NextPrime[Depth[#] - 1] - Depth[#] + 1] &, x, 5]





                                                  share|improve this answer









                                                  $endgroup$



                                                  One can in fact use NestWhile along with NestList, in the same spirit as this previous answer:



                                                  NestList[NestWhile[f, f[#], ! PrimeQ[Depth[#] - 1] &] &, x, 5]
                                                  {x, f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]],
                                                  f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}


                                                  Here is another variation which yields the same result:



                                                  NestList[Nest[f, #, NextPrime[Depth[#] - 1] - Depth[#] + 1] &, x, 5]






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered 5 hours ago









                                                  J. M. is computer-lessJ. M. is computer-less

                                                  96.7k10303461




                                                  96.7k10303461























                                                      1












                                                      $begingroup$

                                                      ClearAll[primesNest0]
                                                      primesNest0[f_, x_, n_] := Nest[f, x, #] & /@ Prime[Range@PrimePi@n]
                                                      primesNest0[f, x, 10]



                                                      {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                                                      Also



                                                      ClearAll[primesNest1]
                                                      primesNest1[f_, x_, n_] := Fold[#2@# &, x, ConstantArray[f, #]] & /@ Prime[Range@PrimePi@n]

                                                      primesNest1[f, x, 10]



                                                      {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                                                      and



                                                      ClearAll[primesNest2]
                                                      primesNest2[f_, x_, n_] := Compose[##&@@ConstantArray[f, #], x] & /@ Prime[Range@PrimePi@n]
                                                      primesNest2[f, x, 10]



                                                      {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                                                      and



                                                      ClearAll[primesNest3]
                                                      primesNest3[f_, x_, n_] := Composition[## & @@ ConstantArray[f, #]][x] & /@
                                                      Prime[Range@PrimePi@n]
                                                      primesNest3[f, x, 10]


                                                      . {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}






                                                      share|improve this answer











                                                      $endgroup$


















                                                        1












                                                        $begingroup$

                                                        ClearAll[primesNest0]
                                                        primesNest0[f_, x_, n_] := Nest[f, x, #] & /@ Prime[Range@PrimePi@n]
                                                        primesNest0[f, x, 10]



                                                        {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                                                        Also



                                                        ClearAll[primesNest1]
                                                        primesNest1[f_, x_, n_] := Fold[#2@# &, x, ConstantArray[f, #]] & /@ Prime[Range@PrimePi@n]

                                                        primesNest1[f, x, 10]



                                                        {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                                                        and



                                                        ClearAll[primesNest2]
                                                        primesNest2[f_, x_, n_] := Compose[##&@@ConstantArray[f, #], x] & /@ Prime[Range@PrimePi@n]
                                                        primesNest2[f, x, 10]



                                                        {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                                                        and



                                                        ClearAll[primesNest3]
                                                        primesNest3[f_, x_, n_] := Composition[## & @@ ConstantArray[f, #]][x] & /@
                                                        Prime[Range@PrimePi@n]
                                                        primesNest3[f, x, 10]


                                                        . {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}






                                                        share|improve this answer











                                                        $endgroup$
















                                                          1












                                                          1








                                                          1





                                                          $begingroup$

                                                          ClearAll[primesNest0]
                                                          primesNest0[f_, x_, n_] := Nest[f, x, #] & /@ Prime[Range@PrimePi@n]
                                                          primesNest0[f, x, 10]



                                                          {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                                                          Also



                                                          ClearAll[primesNest1]
                                                          primesNest1[f_, x_, n_] := Fold[#2@# &, x, ConstantArray[f, #]] & /@ Prime[Range@PrimePi@n]

                                                          primesNest1[f, x, 10]



                                                          {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                                                          and



                                                          ClearAll[primesNest2]
                                                          primesNest2[f_, x_, n_] := Compose[##&@@ConstantArray[f, #], x] & /@ Prime[Range@PrimePi@n]
                                                          primesNest2[f, x, 10]



                                                          {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                                                          and



                                                          ClearAll[primesNest3]
                                                          primesNest3[f_, x_, n_] := Composition[## & @@ ConstantArray[f, #]][x] & /@
                                                          Prime[Range@PrimePi@n]
                                                          primesNest3[f, x, 10]


                                                          . {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}






                                                          share|improve this answer











                                                          $endgroup$



                                                          ClearAll[primesNest0]
                                                          primesNest0[f_, x_, n_] := Nest[f, x, #] & /@ Prime[Range@PrimePi@n]
                                                          primesNest0[f, x, 10]



                                                          {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                                                          Also



                                                          ClearAll[primesNest1]
                                                          primesNest1[f_, x_, n_] := Fold[#2@# &, x, ConstantArray[f, #]] & /@ Prime[Range@PrimePi@n]

                                                          primesNest1[f, x, 10]



                                                          {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                                                          and



                                                          ClearAll[primesNest2]
                                                          primesNest2[f_, x_, n_] := Compose[##&@@ConstantArray[f, #], x] & /@ Prime[Range@PrimePi@n]
                                                          primesNest2[f, x, 10]



                                                          {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}




                                                          and



                                                          ClearAll[primesNest3]
                                                          primesNest3[f_, x_, n_] := Composition[## & @@ ConstantArray[f, #]][x] & /@
                                                          Prime[Range@PrimePi@n]
                                                          primesNest3[f, x, 10]


                                                          . {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}







                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited 6 hours ago

























                                                          answered 6 hours ago









                                                          kglrkglr

                                                          184k10202420




                                                          184k10202420























                                                              0












                                                              $begingroup$

                                                              This may not be an elegant and smart way to do it but here is one way.



                                                                exp = NestList[g, x, 12];
                                                              selector = PrimeQ[LeafCount /@ exp - 1];
                                                              Pick[exp, selector] /. {g -> f, x -> x0}



                                                              {f[f[x0]], f[f[f[x0]]], f[f[f[f[f[x0]]]]], f[f[f[f[f[f[f[x0]]]]]]],
                                                              f[f[f[f[f[f[f[f[f[f[f[x0]]]]]]]]]]]}







                                                              share|improve this answer











                                                              $endgroup$


















                                                                0












                                                                $begingroup$

                                                                This may not be an elegant and smart way to do it but here is one way.



                                                                  exp = NestList[g, x, 12];
                                                                selector = PrimeQ[LeafCount /@ exp - 1];
                                                                Pick[exp, selector] /. {g -> f, x -> x0}



                                                                {f[f[x0]], f[f[f[x0]]], f[f[f[f[f[x0]]]]], f[f[f[f[f[f[f[x0]]]]]]],
                                                                f[f[f[f[f[f[f[f[f[f[f[x0]]]]]]]]]]]}







                                                                share|improve this answer











                                                                $endgroup$
















                                                                  0












                                                                  0








                                                                  0





                                                                  $begingroup$

                                                                  This may not be an elegant and smart way to do it but here is one way.



                                                                    exp = NestList[g, x, 12];
                                                                  selector = PrimeQ[LeafCount /@ exp - 1];
                                                                  Pick[exp, selector] /. {g -> f, x -> x0}



                                                                  {f[f[x0]], f[f[f[x0]]], f[f[f[f[f[x0]]]]], f[f[f[f[f[f[f[x0]]]]]]],
                                                                  f[f[f[f[f[f[f[f[f[f[f[x0]]]]]]]]]]]}







                                                                  share|improve this answer











                                                                  $endgroup$



                                                                  This may not be an elegant and smart way to do it but here is one way.



                                                                    exp = NestList[g, x, 12];
                                                                  selector = PrimeQ[LeafCount /@ exp - 1];
                                                                  Pick[exp, selector] /. {g -> f, x -> x0}



                                                                  {f[f[x0]], f[f[f[x0]]], f[f[f[f[f[x0]]]]], f[f[f[f[f[f[f[x0]]]]]]],
                                                                  f[f[f[f[f[f[f[f[f[f[f[x0]]]]]]]]]]]}








                                                                  share|improve this answer














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited 8 hours ago

























                                                                  answered 8 hours ago









                                                                  Okkes DulgerciOkkes Dulgerci

                                                                  5,0021817




                                                                  5,0021817























                                                                      0












                                                                      $begingroup$

                                                                      Well, maybe not as elegant as with NestList, but for the first five primes (n=5), for example, here is another way



                                                                      n = 5;
                                                                      ls = {};
                                                                      For [k = 0, k < n, k++,
                                                                      (
                                                                      Q = f[x];
                                                                      For [j = 1, j < Prime[k + 1], j++,
                                                                      Q = Map[f, Q];
                                                                      ];
                                                                      ls = Join[ls, {Q}]
                                                                      )
                                                                      ]
                                                                      Print[ls]


                                                                      Here is the result:



                                                                      {f[f[x]],f[f[f[x]]],f[f[f[f[f[x]]]]],f[f[f[f[f[f[f[x]]]]]]],f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}





                                                                      share|improve this answer










                                                                      New contributor




                                                                      mjw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.






                                                                      $endgroup$









                                                                      • 1




                                                                        $begingroup$
                                                                        Why should I avoid the For loop in Mathematica?
                                                                        $endgroup$
                                                                        – corey979
                                                                        8 hours ago










                                                                      • $begingroup$
                                                                        Yes, I've seen that! And I've got two loops! If n>10^4, great point! But for n=5 or n=10, do we care?, this code ran pretty quickly.
                                                                        $endgroup$
                                                                        – mjw
                                                                        7 hours ago












                                                                      • $begingroup$
                                                                        Thank you for the tip, here it is with For replaced by Do: n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
                                                                        $endgroup$
                                                                        – mjw
                                                                        7 hours ago


















                                                                      0












                                                                      $begingroup$

                                                                      Well, maybe not as elegant as with NestList, but for the first five primes (n=5), for example, here is another way



                                                                      n = 5;
                                                                      ls = {};
                                                                      For [k = 0, k < n, k++,
                                                                      (
                                                                      Q = f[x];
                                                                      For [j = 1, j < Prime[k + 1], j++,
                                                                      Q = Map[f, Q];
                                                                      ];
                                                                      ls = Join[ls, {Q}]
                                                                      )
                                                                      ]
                                                                      Print[ls]


                                                                      Here is the result:



                                                                      {f[f[x]],f[f[f[x]]],f[f[f[f[f[x]]]]],f[f[f[f[f[f[f[x]]]]]]],f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}





                                                                      share|improve this answer










                                                                      New contributor




                                                                      mjw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.






                                                                      $endgroup$









                                                                      • 1




                                                                        $begingroup$
                                                                        Why should I avoid the For loop in Mathematica?
                                                                        $endgroup$
                                                                        – corey979
                                                                        8 hours ago










                                                                      • $begingroup$
                                                                        Yes, I've seen that! And I've got two loops! If n>10^4, great point! But for n=5 or n=10, do we care?, this code ran pretty quickly.
                                                                        $endgroup$
                                                                        – mjw
                                                                        7 hours ago












                                                                      • $begingroup$
                                                                        Thank you for the tip, here it is with For replaced by Do: n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
                                                                        $endgroup$
                                                                        – mjw
                                                                        7 hours ago
















                                                                      0












                                                                      0








                                                                      0





                                                                      $begingroup$

                                                                      Well, maybe not as elegant as with NestList, but for the first five primes (n=5), for example, here is another way



                                                                      n = 5;
                                                                      ls = {};
                                                                      For [k = 0, k < n, k++,
                                                                      (
                                                                      Q = f[x];
                                                                      For [j = 1, j < Prime[k + 1], j++,
                                                                      Q = Map[f, Q];
                                                                      ];
                                                                      ls = Join[ls, {Q}]
                                                                      )
                                                                      ]
                                                                      Print[ls]


                                                                      Here is the result:



                                                                      {f[f[x]],f[f[f[x]]],f[f[f[f[f[x]]]]],f[f[f[f[f[f[f[x]]]]]]],f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}





                                                                      share|improve this answer










                                                                      New contributor




                                                                      mjw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.






                                                                      $endgroup$



                                                                      Well, maybe not as elegant as with NestList, but for the first five primes (n=5), for example, here is another way



                                                                      n = 5;
                                                                      ls = {};
                                                                      For [k = 0, k < n, k++,
                                                                      (
                                                                      Q = f[x];
                                                                      For [j = 1, j < Prime[k + 1], j++,
                                                                      Q = Map[f, Q];
                                                                      ];
                                                                      ls = Join[ls, {Q}]
                                                                      )
                                                                      ]
                                                                      Print[ls]


                                                                      Here is the result:



                                                                      {f[f[x]],f[f[f[x]]],f[f[f[f[f[x]]]]],f[f[f[f[f[f[f[x]]]]]]],f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}






                                                                      share|improve this answer










                                                                      New contributor




                                                                      mjw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.









                                                                      share|improve this answer



                                                                      share|improve this answer








                                                                      edited 7 hours ago





















                                                                      New contributor




                                                                      mjw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.









                                                                      answered 8 hours ago









                                                                      mjwmjw

                                                                      1065




                                                                      1065




                                                                      New contributor




                                                                      mjw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.





                                                                      New contributor





                                                                      mjw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.






                                                                      mjw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.








                                                                      • 1




                                                                        $begingroup$
                                                                        Why should I avoid the For loop in Mathematica?
                                                                        $endgroup$
                                                                        – corey979
                                                                        8 hours ago










                                                                      • $begingroup$
                                                                        Yes, I've seen that! And I've got two loops! If n>10^4, great point! But for n=5 or n=10, do we care?, this code ran pretty quickly.
                                                                        $endgroup$
                                                                        – mjw
                                                                        7 hours ago












                                                                      • $begingroup$
                                                                        Thank you for the tip, here it is with For replaced by Do: n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
                                                                        $endgroup$
                                                                        – mjw
                                                                        7 hours ago
















                                                                      • 1




                                                                        $begingroup$
                                                                        Why should I avoid the For loop in Mathematica?
                                                                        $endgroup$
                                                                        – corey979
                                                                        8 hours ago










                                                                      • $begingroup$
                                                                        Yes, I've seen that! And I've got two loops! If n>10^4, great point! But for n=5 or n=10, do we care?, this code ran pretty quickly.
                                                                        $endgroup$
                                                                        – mjw
                                                                        7 hours ago












                                                                      • $begingroup$
                                                                        Thank you for the tip, here it is with For replaced by Do: n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
                                                                        $endgroup$
                                                                        – mjw
                                                                        7 hours ago










                                                                      1




                                                                      1




                                                                      $begingroup$
                                                                      Why should I avoid the For loop in Mathematica?
                                                                      $endgroup$
                                                                      – corey979
                                                                      8 hours ago




                                                                      $begingroup$
                                                                      Why should I avoid the For loop in Mathematica?
                                                                      $endgroup$
                                                                      – corey979
                                                                      8 hours ago












                                                                      $begingroup$
                                                                      Yes, I've seen that! And I've got two loops! If n>10^4, great point! But for n=5 or n=10, do we care?, this code ran pretty quickly.
                                                                      $endgroup$
                                                                      – mjw
                                                                      7 hours ago






                                                                      $begingroup$
                                                                      Yes, I've seen that! And I've got two loops! If n>10^4, great point! But for n=5 or n=10, do we care?, this code ran pretty quickly.
                                                                      $endgroup$
                                                                      – mjw
                                                                      7 hours ago














                                                                      $begingroup$
                                                                      Thank you for the tip, here it is with For replaced by Do: n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
                                                                      $endgroup$
                                                                      – mjw
                                                                      7 hours ago






                                                                      $begingroup$
                                                                      Thank you for the tip, here it is with For replaced by Do: n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
                                                                      $endgroup$
                                                                      – mjw
                                                                      7 hours ago




















                                                                      draft saved

                                                                      draft discarded




















































                                                                      Thanks for contributing an answer to Mathematica Stack Exchange!


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid



                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                      Use MathJax to format equations. MathJax reference.


                                                                      To learn more, see our tips on writing great answers.




                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function () {
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191716%2fcomputing-only-prime-powers-with-nestlist%23new-answer', 'question_page');
                                                                      }
                                                                      );

                                                                      Post as a guest















                                                                      Required, but never shown





















































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown

































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown







                                                                      Popular posts from this blog

                                                                      Fluorita

                                                                      Hulsita

                                                                      Península de Txukotka