Computing only Prime Powers with NestList
$begingroup$
Is there a simple way to compute only prime powers of a function f with NestList? That is, I want to compute:
{f[x_0], f^2[x_0]=f[f[x_0]], f^3[x_0], f^5[x_0]...}
up to some specified prime power. Thanks so much!
recursion
$endgroup$
add a comment |
$begingroup$
Is there a simple way to compute only prime powers of a function f with NestList? That is, I want to compute:
{f[x_0], f^2[x_0]=f[f[x_0]], f^3[x_0], f^5[x_0]...}
up to some specified prime power. Thanks so much!
recursion
$endgroup$
add a comment |
$begingroup$
Is there a simple way to compute only prime powers of a function f with NestList? That is, I want to compute:
{f[x_0], f^2[x_0]=f[f[x_0]], f^3[x_0], f^5[x_0]...}
up to some specified prime power. Thanks so much!
recursion
$endgroup$
Is there a simple way to compute only prime powers of a function f with NestList? That is, I want to compute:
{f[x_0], f^2[x_0]=f[f[x_0]], f^3[x_0], f^5[x_0]...}
up to some specified prime power. Thanks so much!
recursion
recursion
asked Feb 17 at 21:11
user413587user413587
825
825
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
$begingroup$
Lets say you have f(x) and you want results up to 10
n=10;
NestList[f,x,n][[#+1]]&/@Prime[Range@PrimePi@n]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
$endgroup$
add a comment |
$begingroup$
Here's a way using Compose:
n = 10; ComposeList[ConstantArray[f, n], f[x]][[#]] & /@ Prime[Range@PrimePi@n]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
$endgroup$
add a comment |
$begingroup$
Here's a way to accumulate the nestings, i.e. use the previous nesting as the starting point for the next one:
primeNest[f_, x_, n_] := FoldList[Nest[f, ##] &, f[f[x]], Differences[Prime[Range[n]]]]
primeNest[f, x, 5]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]],
f[f[f[f[f[f[f[x]]]]]]], f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
(Depth /@ primeNest[f, x, 20]) - 1
{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}
$endgroup$
add a comment |
$begingroup$
One can in fact use NestWhile
along with NestList
, in the same spirit as this previous answer:
NestList[NestWhile[f, f[#], ! PrimeQ[Depth[#] - 1] &] &, x, 5]
{x, f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]],
f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
Here is another variation which yields the same result:
NestList[Nest[f, #, NextPrime[Depth[#] - 1] - Depth[#] + 1] &, x, 5]
The last few solutions have the flaw of relying on Depth
, which is not very useful if the function to be iterated is even moderately complicated. Nevertheless, these can be modified like so:
NestPrimeList[f_, x_, n_Integer?NonNegative] := Module[{p = 0, r = x, $1},
Prepend[Reap[Do[Sow[r = Nest[f, r, -p + (p = NextPrime[p])], $1], {n}], $1][[2, 1]], x]]
Example:
Exponent[#, x] & /@ NestPrimeList[x # + 1 &, 1, 5]
{0, 2, 3, 5, 7, 11}
$endgroup$
add a comment |
$begingroup$
This may not be an elegant and smart way to do it but here is one way.
exp = NestList[g, x, 12];
selector = PrimeQ[LeafCount /@ exp - 1];
Pick[exp, selector] /. {g -> f, x -> x0}
{f[f[x0]], f[f[f[x0]]], f[f[f[f[f[x0]]]]], f[f[f[f[f[f[f[x0]]]]]]],
f[f[f[f[f[f[f[f[f[f[f[x0]]]]]]]]]]]}
$endgroup$
add a comment |
$begingroup$
ClearAll[primesNest0]
primesNest0[f_, x_, n_] := Nest[f, x, #] & /@ Prime[Range@PrimePi@n]
primesNest0[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
Also
ClearAll[primesNest1]
primesNest1[f_, x_, n_] := Fold[#2@# &, x, ConstantArray[f, #]] & /@ Prime[Range@PrimePi@n]
primesNest1[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
and
ClearAll[primesNest2]
primesNest2[f_, x_, n_] := Compose[##&@@ConstantArray[f, #], x] & /@ Prime[Range@PrimePi@n]
primesNest2[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
and
ClearAll[primesNest3]
primesNest3[f_, x_, n_] := Composition[## & @@ ConstantArray[f, #]][x] & /@
Prime[Range@PrimePi@n]
primesNest3[f, x, 10]
. {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
$endgroup$
add a comment |
$begingroup$
After a For
loop faux pas, and after receiving the splendid advice below came up with this ...
n = 5;
ls = {};
Table[(Q = f[x];
Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}];
ls = Join[ls, {Q}];), {k, Range[n]}];
Print[ls]
Here is the result:
{f[f[x]],f[f[f[x]]],f[f[f[f[f[x]]]]],f[f[f[f[f[f[f[x]]]]]]],f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
Perhaps it is time to Table
this discussion:
n = 5;
ls = {};
Table[(Q = f[x];
Table[Q = Map[f, Q], {j, Range[Prime[k] - 1]}];
ls = Join[ls, {Q}]), {k, Range[n]}];
Print[ls];
$endgroup$
1
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
Feb 17 at 22:24
$begingroup$
Thank you for the tip, here it is withFor
replaced byDo
:n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
$endgroup$
– mjw
Feb 17 at 23:26
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191716%2fcomputing-only-prime-powers-with-nestlist%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Lets say you have f(x) and you want results up to 10
n=10;
NestList[f,x,n][[#+1]]&/@Prime[Range@PrimePi@n]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
$endgroup$
add a comment |
$begingroup$
Lets say you have f(x) and you want results up to 10
n=10;
NestList[f,x,n][[#+1]]&/@Prime[Range@PrimePi@n]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
$endgroup$
add a comment |
$begingroup$
Lets say you have f(x) and you want results up to 10
n=10;
NestList[f,x,n][[#+1]]&/@Prime[Range@PrimePi@n]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
$endgroup$
Lets say you have f(x) and you want results up to 10
n=10;
NestList[f,x,n][[#+1]]&/@Prime[Range@PrimePi@n]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
edited Feb 17 at 22:06
answered Feb 17 at 22:01
J42161217J42161217
3,968323
3,968323
add a comment |
add a comment |
$begingroup$
Here's a way using Compose:
n = 10; ComposeList[ConstantArray[f, n], f[x]][[#]] & /@ Prime[Range@PrimePi@n]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
$endgroup$
add a comment |
$begingroup$
Here's a way using Compose:
n = 10; ComposeList[ConstantArray[f, n], f[x]][[#]] & /@ Prime[Range@PrimePi@n]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
$endgroup$
add a comment |
$begingroup$
Here's a way using Compose:
n = 10; ComposeList[ConstantArray[f, n], f[x]][[#]] & /@ Prime[Range@PrimePi@n]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
$endgroup$
Here's a way using Compose:
n = 10; ComposeList[ConstantArray[f, n], f[x]][[#]] & /@ Prime[Range@PrimePi@n]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
answered Feb 17 at 23:51
bill sbill s
54.4k377156
54.4k377156
add a comment |
add a comment |
$begingroup$
Here's a way to accumulate the nestings, i.e. use the previous nesting as the starting point for the next one:
primeNest[f_, x_, n_] := FoldList[Nest[f, ##] &, f[f[x]], Differences[Prime[Range[n]]]]
primeNest[f, x, 5]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]],
f[f[f[f[f[f[f[x]]]]]]], f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
(Depth /@ primeNest[f, x, 20]) - 1
{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}
$endgroup$
add a comment |
$begingroup$
Here's a way to accumulate the nestings, i.e. use the previous nesting as the starting point for the next one:
primeNest[f_, x_, n_] := FoldList[Nest[f, ##] &, f[f[x]], Differences[Prime[Range[n]]]]
primeNest[f, x, 5]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]],
f[f[f[f[f[f[f[x]]]]]]], f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
(Depth /@ primeNest[f, x, 20]) - 1
{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}
$endgroup$
add a comment |
$begingroup$
Here's a way to accumulate the nestings, i.e. use the previous nesting as the starting point for the next one:
primeNest[f_, x_, n_] := FoldList[Nest[f, ##] &, f[f[x]], Differences[Prime[Range[n]]]]
primeNest[f, x, 5]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]],
f[f[f[f[f[f[f[x]]]]]]], f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
(Depth /@ primeNest[f, x, 20]) - 1
{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}
$endgroup$
Here's a way to accumulate the nestings, i.e. use the previous nesting as the starting point for the next one:
primeNest[f_, x_, n_] := FoldList[Nest[f, ##] &, f[f[x]], Differences[Prime[Range[n]]]]
primeNest[f, x, 5]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]],
f[f[f[f[f[f[f[x]]]]]]], f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
(Depth /@ primeNest[f, x, 20]) - 1
{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}
answered Feb 18 at 0:21
Chip HurstChip Hurst
22.5k15892
22.5k15892
add a comment |
add a comment |
$begingroup$
One can in fact use NestWhile
along with NestList
, in the same spirit as this previous answer:
NestList[NestWhile[f, f[#], ! PrimeQ[Depth[#] - 1] &] &, x, 5]
{x, f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]],
f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
Here is another variation which yields the same result:
NestList[Nest[f, #, NextPrime[Depth[#] - 1] - Depth[#] + 1] &, x, 5]
The last few solutions have the flaw of relying on Depth
, which is not very useful if the function to be iterated is even moderately complicated. Nevertheless, these can be modified like so:
NestPrimeList[f_, x_, n_Integer?NonNegative] := Module[{p = 0, r = x, $1},
Prepend[Reap[Do[Sow[r = Nest[f, r, -p + (p = NextPrime[p])], $1], {n}], $1][[2, 1]], x]]
Example:
Exponent[#, x] & /@ NestPrimeList[x # + 1 &, 1, 5]
{0, 2, 3, 5, 7, 11}
$endgroup$
add a comment |
$begingroup$
One can in fact use NestWhile
along with NestList
, in the same spirit as this previous answer:
NestList[NestWhile[f, f[#], ! PrimeQ[Depth[#] - 1] &] &, x, 5]
{x, f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]],
f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
Here is another variation which yields the same result:
NestList[Nest[f, #, NextPrime[Depth[#] - 1] - Depth[#] + 1] &, x, 5]
The last few solutions have the flaw of relying on Depth
, which is not very useful if the function to be iterated is even moderately complicated. Nevertheless, these can be modified like so:
NestPrimeList[f_, x_, n_Integer?NonNegative] := Module[{p = 0, r = x, $1},
Prepend[Reap[Do[Sow[r = Nest[f, r, -p + (p = NextPrime[p])], $1], {n}], $1][[2, 1]], x]]
Example:
Exponent[#, x] & /@ NestPrimeList[x # + 1 &, 1, 5]
{0, 2, 3, 5, 7, 11}
$endgroup$
add a comment |
$begingroup$
One can in fact use NestWhile
along with NestList
, in the same spirit as this previous answer:
NestList[NestWhile[f, f[#], ! PrimeQ[Depth[#] - 1] &] &, x, 5]
{x, f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]],
f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
Here is another variation which yields the same result:
NestList[Nest[f, #, NextPrime[Depth[#] - 1] - Depth[#] + 1] &, x, 5]
The last few solutions have the flaw of relying on Depth
, which is not very useful if the function to be iterated is even moderately complicated. Nevertheless, these can be modified like so:
NestPrimeList[f_, x_, n_Integer?NonNegative] := Module[{p = 0, r = x, $1},
Prepend[Reap[Do[Sow[r = Nest[f, r, -p + (p = NextPrime[p])], $1], {n}], $1][[2, 1]], x]]
Example:
Exponent[#, x] & /@ NestPrimeList[x # + 1 &, 1, 5]
{0, 2, 3, 5, 7, 11}
$endgroup$
One can in fact use NestWhile
along with NestList
, in the same spirit as this previous answer:
NestList[NestWhile[f, f[#], ! PrimeQ[Depth[#] - 1] &] &, x, 5]
{x, f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]],
f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
Here is another variation which yields the same result:
NestList[Nest[f, #, NextPrime[Depth[#] - 1] - Depth[#] + 1] &, x, 5]
The last few solutions have the flaw of relying on Depth
, which is not very useful if the function to be iterated is even moderately complicated. Nevertheless, these can be modified like so:
NestPrimeList[f_, x_, n_Integer?NonNegative] := Module[{p = 0, r = x, $1},
Prepend[Reap[Do[Sow[r = Nest[f, r, -p + (p = NextPrime[p])], $1], {n}], $1][[2, 1]], x]]
Example:
Exponent[#, x] & /@ NestPrimeList[x # + 1 &, 1, 5]
{0, 2, 3, 5, 7, 11}
edited Feb 19 at 2:24
answered Feb 18 at 0:57
J. M. is slightly pensive♦J. M. is slightly pensive
98.1k10305465
98.1k10305465
add a comment |
add a comment |
$begingroup$
This may not be an elegant and smart way to do it but here is one way.
exp = NestList[g, x, 12];
selector = PrimeQ[LeafCount /@ exp - 1];
Pick[exp, selector] /. {g -> f, x -> x0}
{f[f[x0]], f[f[f[x0]]], f[f[f[f[f[x0]]]]], f[f[f[f[f[f[f[x0]]]]]]],
f[f[f[f[f[f[f[f[f[f[f[x0]]]]]]]]]]]}
$endgroup$
add a comment |
$begingroup$
This may not be an elegant and smart way to do it but here is one way.
exp = NestList[g, x, 12];
selector = PrimeQ[LeafCount /@ exp - 1];
Pick[exp, selector] /. {g -> f, x -> x0}
{f[f[x0]], f[f[f[x0]]], f[f[f[f[f[x0]]]]], f[f[f[f[f[f[f[x0]]]]]]],
f[f[f[f[f[f[f[f[f[f[f[x0]]]]]]]]]]]}
$endgroup$
add a comment |
$begingroup$
This may not be an elegant and smart way to do it but here is one way.
exp = NestList[g, x, 12];
selector = PrimeQ[LeafCount /@ exp - 1];
Pick[exp, selector] /. {g -> f, x -> x0}
{f[f[x0]], f[f[f[x0]]], f[f[f[f[f[x0]]]]], f[f[f[f[f[f[f[x0]]]]]]],
f[f[f[f[f[f[f[f[f[f[f[x0]]]]]]]]]]]}
$endgroup$
This may not be an elegant and smart way to do it but here is one way.
exp = NestList[g, x, 12];
selector = PrimeQ[LeafCount /@ exp - 1];
Pick[exp, selector] /. {g -> f, x -> x0}
{f[f[x0]], f[f[f[x0]]], f[f[f[f[f[x0]]]]], f[f[f[f[f[f[f[x0]]]]]]],
f[f[f[f[f[f[f[f[f[f[f[x0]]]]]]]]]]]}
edited Feb 17 at 22:16
answered Feb 17 at 21:59
Okkes DulgerciOkkes Dulgerci
5,4141919
5,4141919
add a comment |
add a comment |
$begingroup$
ClearAll[primesNest0]
primesNest0[f_, x_, n_] := Nest[f, x, #] & /@ Prime[Range@PrimePi@n]
primesNest0[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
Also
ClearAll[primesNest1]
primesNest1[f_, x_, n_] := Fold[#2@# &, x, ConstantArray[f, #]] & /@ Prime[Range@PrimePi@n]
primesNest1[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
and
ClearAll[primesNest2]
primesNest2[f_, x_, n_] := Compose[##&@@ConstantArray[f, #], x] & /@ Prime[Range@PrimePi@n]
primesNest2[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
and
ClearAll[primesNest3]
primesNest3[f_, x_, n_] := Composition[## & @@ ConstantArray[f, #]][x] & /@
Prime[Range@PrimePi@n]
primesNest3[f, x, 10]
. {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
$endgroup$
add a comment |
$begingroup$
ClearAll[primesNest0]
primesNest0[f_, x_, n_] := Nest[f, x, #] & /@ Prime[Range@PrimePi@n]
primesNest0[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
Also
ClearAll[primesNest1]
primesNest1[f_, x_, n_] := Fold[#2@# &, x, ConstantArray[f, #]] & /@ Prime[Range@PrimePi@n]
primesNest1[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
and
ClearAll[primesNest2]
primesNest2[f_, x_, n_] := Compose[##&@@ConstantArray[f, #], x] & /@ Prime[Range@PrimePi@n]
primesNest2[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
and
ClearAll[primesNest3]
primesNest3[f_, x_, n_] := Composition[## & @@ ConstantArray[f, #]][x] & /@
Prime[Range@PrimePi@n]
primesNest3[f, x, 10]
. {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
$endgroup$
add a comment |
$begingroup$
ClearAll[primesNest0]
primesNest0[f_, x_, n_] := Nest[f, x, #] & /@ Prime[Range@PrimePi@n]
primesNest0[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
Also
ClearAll[primesNest1]
primesNest1[f_, x_, n_] := Fold[#2@# &, x, ConstantArray[f, #]] & /@ Prime[Range@PrimePi@n]
primesNest1[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
and
ClearAll[primesNest2]
primesNest2[f_, x_, n_] := Compose[##&@@ConstantArray[f, #], x] & /@ Prime[Range@PrimePi@n]
primesNest2[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
and
ClearAll[primesNest3]
primesNest3[f_, x_, n_] := Composition[## & @@ ConstantArray[f, #]][x] & /@
Prime[Range@PrimePi@n]
primesNest3[f, x, 10]
. {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
$endgroup$
ClearAll[primesNest0]
primesNest0[f_, x_, n_] := Nest[f, x, #] & /@ Prime[Range@PrimePi@n]
primesNest0[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
Also
ClearAll[primesNest1]
primesNest1[f_, x_, n_] := Fold[#2@# &, x, ConstantArray[f, #]] & /@ Prime[Range@PrimePi@n]
primesNest1[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
and
ClearAll[primesNest2]
primesNest2[f_, x_, n_] := Compose[##&@@ConstantArray[f, #], x] & /@ Prime[Range@PrimePi@n]
primesNest2[f, x, 10]
{f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
and
ClearAll[primesNest3]
primesNest3[f_, x_, n_] := Composition[## & @@ ConstantArray[f, #]][x] & /@
Prime[Range@PrimePi@n]
primesNest3[f, x, 10]
. {f[f[x]], f[f[f[x]]], f[f[f[f[f[x]]]]], f[f[f[f[f[f[f[x]]]]]]]}
edited Feb 18 at 0:09
answered Feb 17 at 23:47
kglrkglr
189k10206424
189k10206424
add a comment |
add a comment |
$begingroup$
After a For
loop faux pas, and after receiving the splendid advice below came up with this ...
n = 5;
ls = {};
Table[(Q = f[x];
Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}];
ls = Join[ls, {Q}];), {k, Range[n]}];
Print[ls]
Here is the result:
{f[f[x]],f[f[f[x]]],f[f[f[f[f[x]]]]],f[f[f[f[f[f[f[x]]]]]]],f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
Perhaps it is time to Table
this discussion:
n = 5;
ls = {};
Table[(Q = f[x];
Table[Q = Map[f, Q], {j, Range[Prime[k] - 1]}];
ls = Join[ls, {Q}]), {k, Range[n]}];
Print[ls];
$endgroup$
1
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
Feb 17 at 22:24
$begingroup$
Thank you for the tip, here it is withFor
replaced byDo
:n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
$endgroup$
– mjw
Feb 17 at 23:26
add a comment |
$begingroup$
After a For
loop faux pas, and after receiving the splendid advice below came up with this ...
n = 5;
ls = {};
Table[(Q = f[x];
Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}];
ls = Join[ls, {Q}];), {k, Range[n]}];
Print[ls]
Here is the result:
{f[f[x]],f[f[f[x]]],f[f[f[f[f[x]]]]],f[f[f[f[f[f[f[x]]]]]]],f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
Perhaps it is time to Table
this discussion:
n = 5;
ls = {};
Table[(Q = f[x];
Table[Q = Map[f, Q], {j, Range[Prime[k] - 1]}];
ls = Join[ls, {Q}]), {k, Range[n]}];
Print[ls];
$endgroup$
1
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
Feb 17 at 22:24
$begingroup$
Thank you for the tip, here it is withFor
replaced byDo
:n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
$endgroup$
– mjw
Feb 17 at 23:26
add a comment |
$begingroup$
After a For
loop faux pas, and after receiving the splendid advice below came up with this ...
n = 5;
ls = {};
Table[(Q = f[x];
Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}];
ls = Join[ls, {Q}];), {k, Range[n]}];
Print[ls]
Here is the result:
{f[f[x]],f[f[f[x]]],f[f[f[f[f[x]]]]],f[f[f[f[f[f[f[x]]]]]]],f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
Perhaps it is time to Table
this discussion:
n = 5;
ls = {};
Table[(Q = f[x];
Table[Q = Map[f, Q], {j, Range[Prime[k] - 1]}];
ls = Join[ls, {Q}]), {k, Range[n]}];
Print[ls];
$endgroup$
After a For
loop faux pas, and after receiving the splendid advice below came up with this ...
n = 5;
ls = {};
Table[(Q = f[x];
Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}];
ls = Join[ls, {Q}];), {k, Range[n]}];
Print[ls]
Here is the result:
{f[f[x]],f[f[f[x]]],f[f[f[f[f[x]]]]],f[f[f[f[f[f[f[x]]]]]]],f[f[f[f[f[f[f[f[f[f[f[x]]]]]]]]]]]}
Perhaps it is time to Table
this discussion:
n = 5;
ls = {};
Table[(Q = f[x];
Table[Q = Map[f, Q], {j, Range[Prime[k] - 1]}];
ls = Join[ls, {Q}]), {k, Range[n]}];
Print[ls];
edited Feb 19 at 1:47
answered Feb 17 at 22:07
mjwmjw
8859
8859
1
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
Feb 17 at 22:24
$begingroup$
Thank you for the tip, here it is withFor
replaced byDo
:n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
$endgroup$
– mjw
Feb 17 at 23:26
add a comment |
1
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
Feb 17 at 22:24
$begingroup$
Thank you for the tip, here it is withFor
replaced byDo
:n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
$endgroup$
– mjw
Feb 17 at 23:26
1
1
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
Feb 17 at 22:24
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
Feb 17 at 22:24
$begingroup$
Thank you for the tip, here it is with
For
replaced by Do
: n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
$endgroup$
– mjw
Feb 17 at 23:26
$begingroup$
Thank you for the tip, here it is with
For
replaced by Do
: n = 5; ls = {}; Do [ ( Q = f[x]; Do [Q = Map[f, Q], {j, Range[Prime[k] - 1]}]; ls = Join[ls, {Q}] ), {k, Range[n]}] Print[ls]
$endgroup$
– mjw
Feb 17 at 23:26
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191716%2fcomputing-only-prime-powers-with-nestlist%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
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