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











up vote
4
down vote

favorite












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.




















  • Possible duplicate of How to generate a table with i != j
    – Michael E2
    23 hours ago















up vote
4
down vote

favorite












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.




















  • Possible duplicate of How to generate a table with i != j
    – Michael E2
    23 hours ago













up vote
4
down vote

favorite









up vote
4
down vote

favorite











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.











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







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 2 days ago









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

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 2 days ago









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.












  • Possible duplicate of How to generate a table with i != j
    – Michael E2
    23 hours ago


















  • Possible duplicate of How to generate a table with i != j
    – Michael E2
    23 hours ago
















Possible duplicate of How to generate a table with i != j
– Michael E2
23 hours ago




Possible duplicate of How to generate a table with i != j
– Michael E2
23 hours ago










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
    2 days ago


















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
    2 days ago


















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























      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',
      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
      });


      }
      });






      Amr is a new contributor. Be nice, and check out our Code of Conduct.










       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f186475%2ftable-2n-1-from-n-2-to-50-except-when-n-3m-1%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      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
        2 days ago















      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
        2 days ago













      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 2 days ago

























      answered 2 days ago









      kglr

      172k8194399




      172k8194399












      • Very clever! +1
        – ciao
        2 days ago


















      • Very clever! +1
        – ciao
        2 days ago
















      Very clever! +1
      – ciao
      2 days ago




      Very clever! +1
      – ciao
      2 days ago










      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
        2 days ago















      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
        2 days ago













      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 2 days ago

























      answered 2 days ago









      bill s

      52.3k375148




      52.3k375148








      • 1




        You could also return Nothing in the else case to remove the unwanted elements directly
        – Lukas Lang
        2 days ago














      • 1




        You could also return Nothing in the else case to remove the unwanted elements directly
        – Lukas Lang
        2 days ago








      1




      1




      You could also return Nothing in the else case to remove the unwanted elements directly
      – Lukas Lang
      2 days ago




      You could also return Nothing in the else case to remove the unwanted elements directly
      – Lukas Lang
      2 days ago










      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 2 days ago

























          answered 2 days ago









          Henrik Schumacher

          45.5k366131




          45.5k366131






















              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 2 days ago

























                  answered 2 days ago









                  chyanog

                  6,80921546




                  6,80921546






















                      Amr is a new contributor. Be nice, and check out our Code of Conduct.










                       

                      draft saved


                      draft discarded


















                      Amr is a new contributor. Be nice, and check out our Code of Conduct.













                      Amr is a new contributor. Be nice, and check out our Code of Conduct.












                      Amr is a new contributor. Be nice, and check out our Code of Conduct.















                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f186475%2ftable-2n-1-from-n-2-to-50-except-when-n-3m-1%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

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy