Substituting 0 for even and 1 for odd natural numbers using EvenQ[]

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











up vote
3
down vote

favorite












I am trying to define a substitution that gives 1 for odd numbers and 0 for even numbers. My guess was (in a particular example)



0, 1, 2, 3, 4, 5, 6 /. N_Integer -> If[EvenQ[N], 0, 1]



But the output is 1,1,1,1,1,1,1. The problem is in EvenQ which is acting on a symbol, but I specified before that it should be applied only to integers, so I do not understand why it does not work.



Where is the problem? Does it have something to do with ->?










share|improve this question







New contributor




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















  • 7




    Yes. Use :> instead of ->. But then, why not use Mod[list, 2]?
    – J. M. is computer-less♦
    7 hours ago















up vote
3
down vote

favorite












I am trying to define a substitution that gives 1 for odd numbers and 0 for even numbers. My guess was (in a particular example)



0, 1, 2, 3, 4, 5, 6 /. N_Integer -> If[EvenQ[N], 0, 1]



But the output is 1,1,1,1,1,1,1. The problem is in EvenQ which is acting on a symbol, but I specified before that it should be applied only to integers, so I do not understand why it does not work.



Where is the problem? Does it have something to do with ->?










share|improve this question







New contributor




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















  • 7




    Yes. Use :> instead of ->. But then, why not use Mod[list, 2]?
    – J. M. is computer-less♦
    7 hours ago













up vote
3
down vote

favorite









up vote
3
down vote

favorite











I am trying to define a substitution that gives 1 for odd numbers and 0 for even numbers. My guess was (in a particular example)



0, 1, 2, 3, 4, 5, 6 /. N_Integer -> If[EvenQ[N], 0, 1]



But the output is 1,1,1,1,1,1,1. The problem is in EvenQ which is acting on a symbol, but I specified before that it should be applied only to integers, so I do not understand why it does not work.



Where is the problem? Does it have something to do with ->?










share|improve this question







New contributor




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











I am trying to define a substitution that gives 1 for odd numbers and 0 for even numbers. My guess was (in a particular example)



0, 1, 2, 3, 4, 5, 6 /. N_Integer -> If[EvenQ[N], 0, 1]



But the output is 1,1,1,1,1,1,1. The problem is in EvenQ which is acting on a symbol, but I specified before that it should be applied only to integers, so I do not understand why it does not work.



Where is the problem? Does it have something to do with ->?







replacement






share|improve this question







New contributor




A. Jimenez Cano 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




A. Jimenez Cano 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






New contributor




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









asked 7 hours ago









A. Jimenez Cano

163




163




New contributor




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





New contributor





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






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







  • 7




    Yes. Use :> instead of ->. But then, why not use Mod[list, 2]?
    – J. M. is computer-less♦
    7 hours ago













  • 7




    Yes. Use :> instead of ->. But then, why not use Mod[list, 2]?
    – J. M. is computer-less♦
    7 hours ago








7




7




Yes. Use :> instead of ->. But then, why not use Mod[list, 2]?
– J. M. is computer-less♦
7 hours ago





Yes. Use :> instead of ->. But then, why not use Mod[list, 2]?
– J. M. is computer-less♦
7 hours ago











2 Answers
2






active

oldest

votes

















up vote
4
down vote













See JM's comment. The following points in a somewhat more general direction for some queries.



Boole@EvenQ@Range[0, 6]





share|improve this answer



























    up vote
    0
    down vote













    In case speed matters Mod suggested by J.M. in comments is orders of magnitude faster than ReplaceAll and Boole:



    SeedRandom[1]
    input = RandomInteger[10^6, 10^6];
    First[RepeatedTiming[res0 = input /. n_Integer :> If[EvenQ[n], 1, 0];]]



    0.836




    First[RepeatedTiming[ res1 = Boole@EvenQ@input; ]]



    .021




    First[RepeatedTiming[res2 = With[True = 1, False = 0, Evaluate@EvenQ[input]]; ]]



    0.047




    First[RepeatedTiming[ res3 = 1 - Mod[input, 2];]] 



    0.0084




    res0 == res1 == res2 == res3
    > True


    where the trick in res2 is from this answer by Mr.Wizard.





    share




















      Your Answer




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

      StackExchange.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: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      A. Jimenez Cano 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%2f184159%2fsubstituting-0-for-even-and-1-for-odd-natural-numbers-using-evenq%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote













      See JM's comment. The following points in a somewhat more general direction for some queries.



      Boole@EvenQ@Range[0, 6]





      share|improve this answer
























        up vote
        4
        down vote













        See JM's comment. The following points in a somewhat more general direction for some queries.



        Boole@EvenQ@Range[0, 6]





        share|improve this answer






















          up vote
          4
          down vote










          up vote
          4
          down vote









          See JM's comment. The following points in a somewhat more general direction for some queries.



          Boole@EvenQ@Range[0, 6]





          share|improve this answer












          See JM's comment. The following points in a somewhat more general direction for some queries.



          Boole@EvenQ@Range[0, 6]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          Alan

          6,0041124




          6,0041124




















              up vote
              0
              down vote













              In case speed matters Mod suggested by J.M. in comments is orders of magnitude faster than ReplaceAll and Boole:



              SeedRandom[1]
              input = RandomInteger[10^6, 10^6];
              First[RepeatedTiming[res0 = input /. n_Integer :> If[EvenQ[n], 1, 0];]]



              0.836




              First[RepeatedTiming[ res1 = Boole@EvenQ@input; ]]



              .021




              First[RepeatedTiming[res2 = With[True = 1, False = 0, Evaluate@EvenQ[input]]; ]]



              0.047




              First[RepeatedTiming[ res3 = 1 - Mod[input, 2];]] 



              0.0084




              res0 == res1 == res2 == res3
              > True


              where the trick in res2 is from this answer by Mr.Wizard.





              share
























                up vote
                0
                down vote













                In case speed matters Mod suggested by J.M. in comments is orders of magnitude faster than ReplaceAll and Boole:



                SeedRandom[1]
                input = RandomInteger[10^6, 10^6];
                First[RepeatedTiming[res0 = input /. n_Integer :> If[EvenQ[n], 1, 0];]]



                0.836




                First[RepeatedTiming[ res1 = Boole@EvenQ@input; ]]



                .021




                First[RepeatedTiming[res2 = With[True = 1, False = 0, Evaluate@EvenQ[input]]; ]]



                0.047




                First[RepeatedTiming[ res3 = 1 - Mod[input, 2];]] 



                0.0084




                res0 == res1 == res2 == res3
                > True


                where the trick in res2 is from this answer by Mr.Wizard.





                share






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  In case speed matters Mod suggested by J.M. in comments is orders of magnitude faster than ReplaceAll and Boole:



                  SeedRandom[1]
                  input = RandomInteger[10^6, 10^6];
                  First[RepeatedTiming[res0 = input /. n_Integer :> If[EvenQ[n], 1, 0];]]



                  0.836




                  First[RepeatedTiming[ res1 = Boole@EvenQ@input; ]]



                  .021




                  First[RepeatedTiming[res2 = With[True = 1, False = 0, Evaluate@EvenQ[input]]; ]]



                  0.047




                  First[RepeatedTiming[ res3 = 1 - Mod[input, 2];]] 



                  0.0084




                  res0 == res1 == res2 == res3
                  > True


                  where the trick in res2 is from this answer by Mr.Wizard.





                  share












                  In case speed matters Mod suggested by J.M. in comments is orders of magnitude faster than ReplaceAll and Boole:



                  SeedRandom[1]
                  input = RandomInteger[10^6, 10^6];
                  First[RepeatedTiming[res0 = input /. n_Integer :> If[EvenQ[n], 1, 0];]]



                  0.836




                  First[RepeatedTiming[ res1 = Boole@EvenQ@input; ]]



                  .021




                  First[RepeatedTiming[res2 = With[True = 1, False = 0, Evaluate@EvenQ[input]]; ]]



                  0.047




                  First[RepeatedTiming[ res3 = 1 - Mod[input, 2];]] 



                  0.0084




                  res0 == res1 == res2 == res3
                  > True


                  where the trick in res2 is from this answer by Mr.Wizard.






                  share











                  share


                  share










                  answered 8 mins ago









                  kglr

                  166k8188389




                  166k8188389




















                      A. Jimenez Cano is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      A. Jimenez Cano is a new contributor. Be nice, and check out our Code of Conduct.












                      A. Jimenez Cano is a new contributor. Be nice, and check out our Code of Conduct.











                      A. Jimenez Cano 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%2f184159%2fsubstituting-0-for-even-and-1-for-odd-natural-numbers-using-evenq%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery