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 9 hours ago
user413587user413587
775
775
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]
$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$
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$
Well, maybe not as elegant as with NestList
, but for the first five primes (n=5), for example, here is another way
n = 5;
ls = {};
For [k = 0, k < n, k++,
(
Q = f[x];
For [j = 1, j < Prime[k + 1], j++,
Q = Map[f, Q];
];
ls = Join[ls, {Q}]
)
]
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]]]]]]]]]]]}
New contributor
$endgroup$
1
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
8 hours ago
$begingroup$
Yes, I've seen that! And I've got two loops! Ifn>10^4
, great point! But forn=5 or n=10
, do we care?, this code ran pretty quickly.
$endgroup$
– mjw
7 hours ago
$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
7 hours ago
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 8 hours ago
answered 8 hours ago
J42161217J42161217
3,787321
3,787321
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 6 hours ago
bill sbill s
53.4k376152
53.4k376152
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 6 hours ago
Chip HurstChip Hurst
21.3k15790
21.3k15790
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]
$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]
$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]
$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]
answered 5 hours ago
J. M. is computer-less♦J. M. is computer-less
96.7k10303461
96.7k10303461
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 6 hours ago
answered 6 hours ago
kglrkglr
184k10202420
184k10202420
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 8 hours ago
answered 8 hours ago
Okkes DulgerciOkkes Dulgerci
5,0021817
5,0021817
add a comment |
add a comment |
$begingroup$
Well, maybe not as elegant as with NestList
, but for the first five primes (n=5), for example, here is another way
n = 5;
ls = {};
For [k = 0, k < n, k++,
(
Q = f[x];
For [j = 1, j < Prime[k + 1], j++,
Q = Map[f, Q];
];
ls = Join[ls, {Q}]
)
]
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]]]]]]]]]]]}
New contributor
$endgroup$
1
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
8 hours ago
$begingroup$
Yes, I've seen that! And I've got two loops! Ifn>10^4
, great point! But forn=5 or n=10
, do we care?, this code ran pretty quickly.
$endgroup$
– mjw
7 hours ago
$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
7 hours ago
add a comment |
$begingroup$
Well, maybe not as elegant as with NestList
, but for the first five primes (n=5), for example, here is another way
n = 5;
ls = {};
For [k = 0, k < n, k++,
(
Q = f[x];
For [j = 1, j < Prime[k + 1], j++,
Q = Map[f, Q];
];
ls = Join[ls, {Q}]
)
]
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]]]]]]]]]]]}
New contributor
$endgroup$
1
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
8 hours ago
$begingroup$
Yes, I've seen that! And I've got two loops! Ifn>10^4
, great point! But forn=5 or n=10
, do we care?, this code ran pretty quickly.
$endgroup$
– mjw
7 hours ago
$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
7 hours ago
add a comment |
$begingroup$
Well, maybe not as elegant as with NestList
, but for the first five primes (n=5), for example, here is another way
n = 5;
ls = {};
For [k = 0, k < n, k++,
(
Q = f[x];
For [j = 1, j < Prime[k + 1], j++,
Q = Map[f, Q];
];
ls = Join[ls, {Q}]
)
]
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]]]]]]]]]]]}
New contributor
$endgroup$
Well, maybe not as elegant as with NestList
, but for the first five primes (n=5), for example, here is another way
n = 5;
ls = {};
For [k = 0, k < n, k++,
(
Q = f[x];
For [j = 1, j < Prime[k + 1], j++,
Q = Map[f, Q];
];
ls = Join[ls, {Q}]
)
]
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]]]]]]]]]]]}
New contributor
edited 7 hours ago
New contributor
answered 8 hours ago
mjwmjw
1065
1065
New contributor
New contributor
1
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
8 hours ago
$begingroup$
Yes, I've seen that! And I've got two loops! Ifn>10^4
, great point! But forn=5 or n=10
, do we care?, this code ran pretty quickly.
$endgroup$
– mjw
7 hours ago
$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
7 hours ago
add a comment |
1
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
8 hours ago
$begingroup$
Yes, I've seen that! And I've got two loops! Ifn>10^4
, great point! But forn=5 or n=10
, do we care?, this code ran pretty quickly.
$endgroup$
– mjw
7 hours ago
$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
7 hours ago
1
1
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
8 hours ago
$begingroup$
Why should I avoid the For loop in Mathematica?
$endgroup$
– corey979
8 hours ago
$begingroup$
Yes, I've seen that! And I've got two loops! If
n>10^4
, great point! But for n=5 or n=10
, do we care?, this code ran pretty quickly.$endgroup$
– mjw
7 hours ago
$begingroup$
Yes, I've seen that! And I've got two loops! If
n>10^4
, great point! But for n=5 or n=10
, do we care?, this code ran pretty quickly.$endgroup$
– mjw
7 hours ago
$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
7 hours ago
$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
7 hours ago
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