Table $ 2n-1 $ from $ n=2 $ to $ 50 $ except when $ n=3m-1 $ [duplicate]

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











up vote
4
down vote

favorite













This question already has an answer here:



  • How to generate a table with i != j

    6 answers



How to Table $ 2n-1 $ for $ n $ from $ 2 $ to $ 50 $, except when $ n=3m-1 $ for another integer $ m $?










share|improve this question









New contributor




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











marked as duplicate by Daniel Lichtblau, Michael E2, Szabolcs, Kuba list-manipulation
Users with the  list-manipulation badge can single-handedly close list-manipulation questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 25 at 21:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















    up vote
    4
    down vote

    favorite













    This question already has an answer here:



    • How to generate a table with i != j

      6 answers



    How to Table $ 2n-1 $ for $ n $ from $ 2 $ to $ 50 $, except when $ n=3m-1 $ for another integer $ m $?










    share|improve this question









    New contributor




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











    marked as duplicate by Daniel Lichtblau, Michael E2, Szabolcs, Kuba list-manipulation
    Users with the  list-manipulation badge can single-handedly close list-manipulation questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Nov 25 at 21:37


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite












      This question already has an answer here:



      • How to generate a table with i != j

        6 answers



      How to Table $ 2n-1 $ for $ n $ from $ 2 $ to $ 50 $, except when $ n=3m-1 $ for another integer $ m $?










      share|improve this question









      New contributor




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












      This question already has an answer here:



      • How to generate a table with i != j

        6 answers



      How to Table $ 2n-1 $ for $ n $ from $ 2 $ to $ 50 $, except when $ n=3m-1 $ for another integer $ m $?





      This question already has an answer here:



      • How to generate a table with i != j

        6 answers







      list-manipulation table






      share|improve this question









      New contributor




      Amr 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 question









      New contributor




      Amr 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 question




      share|improve this question








      edited Nov 22 at 12:12









      Αλέξανδρος Ζεγγ

      3,5281927




      3,5281927






      New contributor




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









      asked Nov 22 at 6:47









      Amr

      212




      212




      New contributor




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





      New contributor





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






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




      marked as duplicate by Daniel Lichtblau, Michael E2, Szabolcs, Kuba list-manipulation
      Users with the  list-manipulation badge can single-handedly close list-manipulation questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 25 at 21:37


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Daniel Lichtblau, Michael E2, Szabolcs, Kuba list-manipulation
      Users with the  list-manipulation badge can single-handedly close list-manipulation questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 25 at 21:37


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          7
          down vote













          Using Drop, Riffle and Complement:



          Drop[Range[5, 2 50 - 1, 2], 3, -1, 3 ]



          5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
          55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




          Riffle[#, # + 2] &[2 Range[3, 50, 3] - 1]



          same result




          2 Complement[#, #[[;; ;; 3]]] & @ Range[2, 50] - 1;



          same result




          All three are faster than Pick+ Unitize combination from Henrik's answer:



          nmax = 1000000;

          e0 = Drop[Range[5, 2 nmax - 1, 2], 3, -1, 3 ]; // AbsoluteTiming



          0.0102131




          e1 = Riffle[#, # + 2] &[2 Range[3, nmax, 3] - 1]; // AbsoluteTiming // First



          0.0126898




          e2 = 2 Complement[#, #[[;; ;; 3]]] & @ Range[2, nmax] - 1; // AbsoluteTiming // First



          0.0354908




          versus Henrik's



          d = Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, nmax]; // AbsoluteTiming // First



          0.0526116




          and chyanog's method:



          (k = Floor[(2 nmax + 1)/3] - 1; 
          res = Range[4, 3 k + 1, 3] + BitAnd[Range[k], 1];) // AbsoluteTiming // First



          0.0125891




          e0 == e1 == e2 == d == res



          True







          share|improve this answer






















          • Very clever! +1
            – ciao
            Nov 22 at 9:30

















          up vote
          5
          down vote













          The condition to avoid can be rewritten using Mod, hence:



          Table[If[Mod[n, 3] != 2, 2 n - 1, Nothing], n, 2, 50]


          Or you can use Map /@ instead of Table:



          If[Mod[#, 3] != 2, 2 # - 1, Nothing] & /@ Range[2, 50]


          (Thanks to Lukas Lang for the easier use of Nothing).






          share|improve this answer


















          • 1




            You could also return Nothing in the else case to remove the unwanted elements directly
            – Lukas Lang
            Nov 22 at 7:03

















          up vote
          4
          down vote













          An idiomatic way with Sow and Reap:



          Reap[
          Do[If[Mod[n, 3] != 2, Sow[2 n - 1]], n, 2, 50]
          ][[2, 1]]



          5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
          55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




          A harder to read, vectorized way: First create the list and pick the valid elements with Pick.



          Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, 50]



          5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
          55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




          The vectorized version is about 40 times faster:



          nmax = 1000000;
          a = Reap[Do[If[Mod[n, 3] != 2, Sow[2 n - 1]], n, 2, nmax]][[2, 1]]; // AbsoluteTiming // First
          b = Table[If[Mod[n, 3] != 2, 2 n - 1, Nothing], n, 2, nmax]; // AbsoluteTiming // First
          c = If[Mod[#, 3] != 2, 2 # - 1, Nothing] & /@ Range[2, nmax]; // AbsoluteTiming // First
          d = Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, nmax]; // AbsoluteTiming // First
          a == b == c == d



          0.943828



          0.907419



          1.34615



          0.018833



          True







          share|improve this answer





























            up vote
            2
            down vote













            More efficient way



            Clear["f*"];
            f1[m_] := Module[r = Range[4, 2 m - Mod[m, 3], 3], r[[1 ;; ;; 2]] += 1; r];
            f2[m_] := Drop[Range[5, 2 m - 1, 2], 3, -1, 3];

            r1 = f1[10^7]; // RepeatedTiming
            r2 = f2[10^7]; // RepeatedTiming

            r1 == r2



            0.11, Null



            0.14, Null



            True







            share|improve this answer





























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              7
              down vote













              Using Drop, Riffle and Complement:



              Drop[Range[5, 2 50 - 1, 2], 3, -1, 3 ]



              5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
              55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




              Riffle[#, # + 2] &[2 Range[3, 50, 3] - 1]



              same result




              2 Complement[#, #[[;; ;; 3]]] & @ Range[2, 50] - 1;



              same result




              All three are faster than Pick+ Unitize combination from Henrik's answer:



              nmax = 1000000;

              e0 = Drop[Range[5, 2 nmax - 1, 2], 3, -1, 3 ]; // AbsoluteTiming



              0.0102131




              e1 = Riffle[#, # + 2] &[2 Range[3, nmax, 3] - 1]; // AbsoluteTiming // First



              0.0126898




              e2 = 2 Complement[#, #[[;; ;; 3]]] & @ Range[2, nmax] - 1; // AbsoluteTiming // First



              0.0354908




              versus Henrik's



              d = Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, nmax]; // AbsoluteTiming // First



              0.0526116




              and chyanog's method:



              (k = Floor[(2 nmax + 1)/3] - 1; 
              res = Range[4, 3 k + 1, 3] + BitAnd[Range[k], 1];) // AbsoluteTiming // First



              0.0125891




              e0 == e1 == e2 == d == res



              True







              share|improve this answer






















              • Very clever! +1
                – ciao
                Nov 22 at 9:30














              up vote
              7
              down vote













              Using Drop, Riffle and Complement:



              Drop[Range[5, 2 50 - 1, 2], 3, -1, 3 ]



              5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
              55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




              Riffle[#, # + 2] &[2 Range[3, 50, 3] - 1]



              same result




              2 Complement[#, #[[;; ;; 3]]] & @ Range[2, 50] - 1;



              same result




              All three are faster than Pick+ Unitize combination from Henrik's answer:



              nmax = 1000000;

              e0 = Drop[Range[5, 2 nmax - 1, 2], 3, -1, 3 ]; // AbsoluteTiming



              0.0102131




              e1 = Riffle[#, # + 2] &[2 Range[3, nmax, 3] - 1]; // AbsoluteTiming // First



              0.0126898




              e2 = 2 Complement[#, #[[;; ;; 3]]] & @ Range[2, nmax] - 1; // AbsoluteTiming // First



              0.0354908




              versus Henrik's



              d = Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, nmax]; // AbsoluteTiming // First



              0.0526116




              and chyanog's method:



              (k = Floor[(2 nmax + 1)/3] - 1; 
              res = Range[4, 3 k + 1, 3] + BitAnd[Range[k], 1];) // AbsoluteTiming // First



              0.0125891




              e0 == e1 == e2 == d == res



              True







              share|improve this answer






















              • Very clever! +1
                – ciao
                Nov 22 at 9:30












              up vote
              7
              down vote










              up vote
              7
              down vote









              Using Drop, Riffle and Complement:



              Drop[Range[5, 2 50 - 1, 2], 3, -1, 3 ]



              5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
              55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




              Riffle[#, # + 2] &[2 Range[3, 50, 3] - 1]



              same result




              2 Complement[#, #[[;; ;; 3]]] & @ Range[2, 50] - 1;



              same result




              All three are faster than Pick+ Unitize combination from Henrik's answer:



              nmax = 1000000;

              e0 = Drop[Range[5, 2 nmax - 1, 2], 3, -1, 3 ]; // AbsoluteTiming



              0.0102131




              e1 = Riffle[#, # + 2] &[2 Range[3, nmax, 3] - 1]; // AbsoluteTiming // First



              0.0126898




              e2 = 2 Complement[#, #[[;; ;; 3]]] & @ Range[2, nmax] - 1; // AbsoluteTiming // First



              0.0354908




              versus Henrik's



              d = Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, nmax]; // AbsoluteTiming // First



              0.0526116




              and chyanog's method:



              (k = Floor[(2 nmax + 1)/3] - 1; 
              res = Range[4, 3 k + 1, 3] + BitAnd[Range[k], 1];) // AbsoluteTiming // First



              0.0125891




              e0 == e1 == e2 == d == res



              True







              share|improve this answer














              Using Drop, Riffle and Complement:



              Drop[Range[5, 2 50 - 1, 2], 3, -1, 3 ]



              5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
              55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




              Riffle[#, # + 2] &[2 Range[3, 50, 3] - 1]



              same result




              2 Complement[#, #[[;; ;; 3]]] & @ Range[2, 50] - 1;



              same result




              All three are faster than Pick+ Unitize combination from Henrik's answer:



              nmax = 1000000;

              e0 = Drop[Range[5, 2 nmax - 1, 2], 3, -1, 3 ]; // AbsoluteTiming



              0.0102131




              e1 = Riffle[#, # + 2] &[2 Range[3, nmax, 3] - 1]; // AbsoluteTiming // First



              0.0126898




              e2 = 2 Complement[#, #[[;; ;; 3]]] & @ Range[2, nmax] - 1; // AbsoluteTiming // First



              0.0354908




              versus Henrik's



              d = Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, nmax]; // AbsoluteTiming // First



              0.0526116




              and chyanog's method:



              (k = Floor[(2 nmax + 1)/3] - 1; 
              res = Range[4, 3 k + 1, 3] + BitAnd[Range[k], 1];) // AbsoluteTiming // First



              0.0125891




              e0 == e1 == e2 == d == res



              True








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 22 at 13:45

























              answered Nov 22 at 8:58









              kglr

              173k8195400




              173k8195400











              • Very clever! +1
                – ciao
                Nov 22 at 9:30
















              • Very clever! +1
                – ciao
                Nov 22 at 9:30















              Very clever! +1
              – ciao
              Nov 22 at 9:30




              Very clever! +1
              – ciao
              Nov 22 at 9:30










              up vote
              5
              down vote













              The condition to avoid can be rewritten using Mod, hence:



              Table[If[Mod[n, 3] != 2, 2 n - 1, Nothing], n, 2, 50]


              Or you can use Map /@ instead of Table:



              If[Mod[#, 3] != 2, 2 # - 1, Nothing] & /@ Range[2, 50]


              (Thanks to Lukas Lang for the easier use of Nothing).






              share|improve this answer


















              • 1




                You could also return Nothing in the else case to remove the unwanted elements directly
                – Lukas Lang
                Nov 22 at 7:03














              up vote
              5
              down vote













              The condition to avoid can be rewritten using Mod, hence:



              Table[If[Mod[n, 3] != 2, 2 n - 1, Nothing], n, 2, 50]


              Or you can use Map /@ instead of Table:



              If[Mod[#, 3] != 2, 2 # - 1, Nothing] & /@ Range[2, 50]


              (Thanks to Lukas Lang for the easier use of Nothing).






              share|improve this answer


















              • 1




                You could also return Nothing in the else case to remove the unwanted elements directly
                – Lukas Lang
                Nov 22 at 7:03












              up vote
              5
              down vote










              up vote
              5
              down vote









              The condition to avoid can be rewritten using Mod, hence:



              Table[If[Mod[n, 3] != 2, 2 n - 1, Nothing], n, 2, 50]


              Or you can use Map /@ instead of Table:



              If[Mod[#, 3] != 2, 2 # - 1, Nothing] & /@ Range[2, 50]


              (Thanks to Lukas Lang for the easier use of Nothing).






              share|improve this answer














              The condition to avoid can be rewritten using Mod, hence:



              Table[If[Mod[n, 3] != 2, 2 n - 1, Nothing], n, 2, 50]


              Or you can use Map /@ instead of Table:



              If[Mod[#, 3] != 2, 2 # - 1, Nothing] & /@ Range[2, 50]


              (Thanks to Lukas Lang for the easier use of Nothing).







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 22 at 14:52

























              answered Nov 22 at 6:56









              bill s

              52.4k375149




              52.4k375149







              • 1




                You could also return Nothing in the else case to remove the unwanted elements directly
                – Lukas Lang
                Nov 22 at 7:03












              • 1




                You could also return Nothing in the else case to remove the unwanted elements directly
                – Lukas Lang
                Nov 22 at 7:03







              1




              1




              You could also return Nothing in the else case to remove the unwanted elements directly
              – Lukas Lang
              Nov 22 at 7:03




              You could also return Nothing in the else case to remove the unwanted elements directly
              – Lukas Lang
              Nov 22 at 7:03










              up vote
              4
              down vote













              An idiomatic way with Sow and Reap:



              Reap[
              Do[If[Mod[n, 3] != 2, Sow[2 n - 1]], n, 2, 50]
              ][[2, 1]]



              5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
              55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




              A harder to read, vectorized way: First create the list and pick the valid elements with Pick.



              Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, 50]



              5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
              55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




              The vectorized version is about 40 times faster:



              nmax = 1000000;
              a = Reap[Do[If[Mod[n, 3] != 2, Sow[2 n - 1]], n, 2, nmax]][[2, 1]]; // AbsoluteTiming // First
              b = Table[If[Mod[n, 3] != 2, 2 n - 1, Nothing], n, 2, nmax]; // AbsoluteTiming // First
              c = If[Mod[#, 3] != 2, 2 # - 1, Nothing] & /@ Range[2, nmax]; // AbsoluteTiming // First
              d = Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, nmax]; // AbsoluteTiming // First
              a == b == c == d



              0.943828



              0.907419



              1.34615



              0.018833



              True







              share|improve this answer


























                up vote
                4
                down vote













                An idiomatic way with Sow and Reap:



                Reap[
                Do[If[Mod[n, 3] != 2, Sow[2 n - 1]], n, 2, 50]
                ][[2, 1]]



                5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
                55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




                A harder to read, vectorized way: First create the list and pick the valid elements with Pick.



                Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, 50]



                5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
                55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




                The vectorized version is about 40 times faster:



                nmax = 1000000;
                a = Reap[Do[If[Mod[n, 3] != 2, Sow[2 n - 1]], n, 2, nmax]][[2, 1]]; // AbsoluteTiming // First
                b = Table[If[Mod[n, 3] != 2, 2 n - 1, Nothing], n, 2, nmax]; // AbsoluteTiming // First
                c = If[Mod[#, 3] != 2, 2 # - 1, Nothing] & /@ Range[2, nmax]; // AbsoluteTiming // First
                d = Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, nmax]; // AbsoluteTiming // First
                a == b == c == d



                0.943828



                0.907419



                1.34615



                0.018833



                True







                share|improve this answer
























                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  An idiomatic way with Sow and Reap:



                  Reap[
                  Do[If[Mod[n, 3] != 2, Sow[2 n - 1]], n, 2, 50]
                  ][[2, 1]]



                  5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
                  55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




                  A harder to read, vectorized way: First create the list and pick the valid elements with Pick.



                  Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, 50]



                  5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
                  55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




                  The vectorized version is about 40 times faster:



                  nmax = 1000000;
                  a = Reap[Do[If[Mod[n, 3] != 2, Sow[2 n - 1]], n, 2, nmax]][[2, 1]]; // AbsoluteTiming // First
                  b = Table[If[Mod[n, 3] != 2, 2 n - 1, Nothing], n, 2, nmax]; // AbsoluteTiming // First
                  c = If[Mod[#, 3] != 2, 2 # - 1, Nothing] & /@ Range[2, nmax]; // AbsoluteTiming // First
                  d = Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, nmax]; // AbsoluteTiming // First
                  a == b == c == d



                  0.943828



                  0.907419



                  1.34615



                  0.018833



                  True







                  share|improve this answer














                  An idiomatic way with Sow and Reap:



                  Reap[
                  Do[If[Mod[n, 3] != 2, Sow[2 n - 1]], n, 2, 50]
                  ][[2, 1]]



                  5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
                  55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




                  A harder to read, vectorized way: First create the list and pick the valid elements with Pick.



                  Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, 50]



                  5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53,
                  55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97




                  The vectorized version is about 40 times faster:



                  nmax = 1000000;
                  a = Reap[Do[If[Mod[n, 3] != 2, Sow[2 n - 1]], n, 2, nmax]][[2, 1]]; // AbsoluteTiming // First
                  b = Table[If[Mod[n, 3] != 2, 2 n - 1, Nothing], n, 2, nmax]; // AbsoluteTiming // First
                  c = If[Mod[#, 3] != 2, 2 # - 1, Nothing] & /@ Range[2, nmax]; // AbsoluteTiming // First
                  d = Pick[2 # - 1, Unitize[Mod[#, 3] - 2], 1] &@Range[2, nmax]; // AbsoluteTiming // First
                  a == b == c == d



                  0.943828



                  0.907419



                  1.34615



                  0.018833



                  True








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 at 15:58

























                  answered Nov 22 at 7:28









                  Henrik Schumacher

                  45.8k466132




                  45.8k466132




















                      up vote
                      2
                      down vote













                      More efficient way



                      Clear["f*"];
                      f1[m_] := Module[r = Range[4, 2 m - Mod[m, 3], 3], r[[1 ;; ;; 2]] += 1; r];
                      f2[m_] := Drop[Range[5, 2 m - 1, 2], 3, -1, 3];

                      r1 = f1[10^7]; // RepeatedTiming
                      r2 = f2[10^7]; // RepeatedTiming

                      r1 == r2



                      0.11, Null



                      0.14, Null



                      True







                      share|improve this answer


























                        up vote
                        2
                        down vote













                        More efficient way



                        Clear["f*"];
                        f1[m_] := Module[r = Range[4, 2 m - Mod[m, 3], 3], r[[1 ;; ;; 2]] += 1; r];
                        f2[m_] := Drop[Range[5, 2 m - 1, 2], 3, -1, 3];

                        r1 = f1[10^7]; // RepeatedTiming
                        r2 = f2[10^7]; // RepeatedTiming

                        r1 == r2



                        0.11, Null



                        0.14, Null



                        True







                        share|improve this answer
























                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          More efficient way



                          Clear["f*"];
                          f1[m_] := Module[r = Range[4, 2 m - Mod[m, 3], 3], r[[1 ;; ;; 2]] += 1; r];
                          f2[m_] := Drop[Range[5, 2 m - 1, 2], 3, -1, 3];

                          r1 = f1[10^7]; // RepeatedTiming
                          r2 = f2[10^7]; // RepeatedTiming

                          r1 == r2



                          0.11, Null



                          0.14, Null



                          True







                          share|improve this answer














                          More efficient way



                          Clear["f*"];
                          f1[m_] := Module[r = Range[4, 2 m - Mod[m, 3], 3], r[[1 ;; ;; 2]] += 1; r];
                          f2[m_] := Drop[Range[5, 2 m - 1, 2], 3, -1, 3];

                          r1 = f1[10^7]; // RepeatedTiming
                          r2 = f2[10^7]; // RepeatedTiming

                          r1 == r2



                          0.11, Null



                          0.14, Null



                          True








                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 23 at 3:23

























                          answered Nov 22 at 11:25









                          chyanog

                          6,80921546




                          6,80921546












                              Popular posts from this blog

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

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay