Table $ 2n-1 $ from $ n=2 $ to $ 50 $ except when $ n=3m-1 $ [duplicate]
$begingroup$
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 $?
list-manipulation table
$endgroup$
marked as duplicate by Daniel Lichtblau, Michael E2, Szabolcs, Kuba♦
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 '18 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.
add a comment |
$begingroup$
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 $?
list-manipulation table
$endgroup$
marked as duplicate by Daniel Lichtblau, Michael E2, Szabolcs, Kuba♦
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 '18 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.
add a comment |
$begingroup$
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 $?
list-manipulation table
$endgroup$
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
list-manipulation table
edited Nov 22 '18 at 12:12
Αλέξανδρος Ζεγγ
4,1541929
4,1541929
asked Nov 22 '18 at 6:47
AmrAmr
212
212
marked as duplicate by Daniel Lichtblau, Michael E2, Szabolcs, Kuba♦
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 '18 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♦
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 '18 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.
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
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
$endgroup$
$begingroup$
Very clever! +1
$endgroup$
– ciao
Nov 22 '18 at 9:30
add a comment |
$begingroup$
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).
$endgroup$
1
$begingroup$
You could also returnNothing
in the else case to remove the unwanted elements directly
$endgroup$
– Lukas Lang
Nov 22 '18 at 7:03
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
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
$endgroup$
$begingroup$
Very clever! +1
$endgroup$
– ciao
Nov 22 '18 at 9:30
add a comment |
$begingroup$
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
$endgroup$
$begingroup$
Very clever! +1
$endgroup$
– ciao
Nov 22 '18 at 9:30
add a comment |
$begingroup$
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
$endgroup$
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
edited Nov 22 '18 at 13:45
answered Nov 22 '18 at 8:58
kglrkglr
179k9198410
179k9198410
$begingroup$
Very clever! +1
$endgroup$
– ciao
Nov 22 '18 at 9:30
add a comment |
$begingroup$
Very clever! +1
$endgroup$
– ciao
Nov 22 '18 at 9:30
$begingroup$
Very clever! +1
$endgroup$
– ciao
Nov 22 '18 at 9:30
$begingroup$
Very clever! +1
$endgroup$
– ciao
Nov 22 '18 at 9:30
add a comment |
$begingroup$
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).
$endgroup$
1
$begingroup$
You could also returnNothing
in the else case to remove the unwanted elements directly
$endgroup$
– Lukas Lang
Nov 22 '18 at 7:03
add a comment |
$begingroup$
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).
$endgroup$
1
$begingroup$
You could also returnNothing
in the else case to remove the unwanted elements directly
$endgroup$
– Lukas Lang
Nov 22 '18 at 7:03
add a comment |
$begingroup$
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).
$endgroup$
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).
edited Nov 22 '18 at 14:52
answered Nov 22 '18 at 6:56
bill sbill s
52.8k375150
52.8k375150
1
$begingroup$
You could also returnNothing
in the else case to remove the unwanted elements directly
$endgroup$
– Lukas Lang
Nov 22 '18 at 7:03
add a comment |
1
$begingroup$
You could also returnNothing
in the else case to remove the unwanted elements directly
$endgroup$
– Lukas Lang
Nov 22 '18 at 7:03
1
1
$begingroup$
You could also return
Nothing
in the else case to remove the unwanted elements directly$endgroup$
– Lukas Lang
Nov 22 '18 at 7:03
$begingroup$
You could also return
Nothing
in the else case to remove the unwanted elements directly$endgroup$
– Lukas Lang
Nov 22 '18 at 7:03
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
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
edited Nov 22 '18 at 15:58
answered Nov 22 '18 at 7:28
Henrik SchumacherHenrik Schumacher
50.4k469144
50.4k469144
add a comment |
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
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
edited Nov 23 '18 at 3:23
answered Nov 22 '18 at 11:25
chyanogchyanog
6,82921546
6,82921546
add a comment |
add a comment |