Substituting 0 for even and 1 for odd natural numbers using EvenQ[]
Clash 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 ->
?
replacement
New contributor
add a comment |Â
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 ->
?
replacement
New contributor
7
Yes. Use:>
instead of->
. But then, why not useMod[list, 2]
?
â J. M. is computer-lessâ¦
7 hours ago
add a comment |Â
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 ->
?
replacement
New contributor
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
replacement
New contributor
New contributor
New contributor
asked 7 hours ago
A. Jimenez Cano
163
163
New contributor
New contributor
7
Yes. Use:>
instead of->
. But then, why not useMod[list, 2]
?
â J. M. is computer-lessâ¦
7 hours ago
add a comment |Â
7
Yes. Use:>
instead of->
. But then, why not useMod[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
add a comment |Â
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]
add a comment |Â
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.
add a comment |Â
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]
add a comment |Â
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]
add a comment |Â
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]
See JM's comment. The following points in a somewhat more general direction for some queries.
Boole@EvenQ@Range[0, 6]
answered 1 hour ago
Alan
6,0041124
6,0041124
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered 9 mins ago
kglr
166k8188389
166k8188389
add a comment |Â
add a comment |Â
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.
A. Jimenez Cano is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
7
Yes. Use
:>
instead of->
. But then, why not useMod[list, 2]
?â J. M. is computer-lessâ¦
7 hours ago