0
$\begingroup$

I wrote this program on Mathematica (which I am very new to):

h[n_] := n + n/FactorInteger[n][[-1, 1]] + 1
L = NestList[h,1,10]
Do[Do[If[MemberQ[L, NestList[h, y, 10][[x]], Print[y, " ", x]],{x, 1, 10}],{y, 1, 10}]

(This code may not be particularly efficient, but it's the best I know how to do.) Which basically calculates how many iterations of h must be applied to y to reach an element of L.

When I run the program, I get:

$$1~1$$ $$1~2$$ $$1~3$$ $$1~4$$ $$1~5$$ $$1~6$$ $$1~7$$ $$1~8$$ $$1~9$$ $$1~10$$ $$2~3$$ $$2~4$$ $$2~5$$ $$...$$ $$10~2$$ $$10~3$$ $$10~4$$ $$10~5$$ $$10~6$$ $$10~7$$ $$...$$

(There should be spaces between the numbers) You can see that the program prints ten values for every y. It probably requires rewriting of the entire program, but I would like to tell it to only print the first value of y, so the above list would become:

$$1~1$$ $$2~3$$ $$...$$ $$10~2$$ $$...$$

The values are probably all under 5, but it is possible that there are some greater than 10. I would also like to remove that limit.

I have no idea how to do this, because I do not know Mathematica well enough yet. I would appreciate help with this very much. Thanks.

P.s.: To help clarify, these are the first few values of the output as I would like them to appear, which I have calculated by hand:

$$1~1$$ $$2~3$$ $$3~1$$ $$4~2$$ $$5~1$$ $$6~2$$ $$7~1$$ $$8~2$$ $$9~1$$ $$10~2$$ $$11~2$$ $$12~3$$ $$...$$

And no, I do not want to save this as a list, unless it can be kept in the above format.

$\endgroup$
3
  • $\begingroup$ If I understood your problem correctly, then you just need to add the conditional operator around the Print[***]. Something like If[y==1,Print[***]] $\endgroup$ – Rom38 Apr 7 '16 at 4:10
  • 1
    $\begingroup$ Do you really just want to print this to the screen? Would you rather have the values saved to a list? $\endgroup$ – march Apr 7 '16 at 4:28
  • $\begingroup$ @Cody H. If you would like to merge these two accounts, please contact a moderator. $\endgroup$ – march Apr 7 '16 at 5:08
2
$\begingroup$
h[n_] := n + n/FactorInteger[n][[-1, 1]] + 1
L = NestList[h, 1, 10]
h[n_] := n + n/FactorInteger[n][[-1, 1]] + 1
firsts[max_Integer] := Module[{L = NestList[h, 1, max]},
  Prepend[FirstPosition[NestList[h, #, max], a_ /; MemberQ[L, a]], #] & /@ Range[max]
 ]
firsts[10]
(* {{1, 1}, {2, 3}, {3, 1}, {4, 2}, {5, 1}, {6, 2}, {7, 1}, {8, 2}, {9, 1}, {10, 2}} *)

Since you insist on that output format:

ToString[#1] <> " " <> ToString[#2] & @@@ firsts[10] // Column

enter image description here

or, better:

TableForm@firsts[10]

enter image description here

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.