Message Boards Message Boards

GROUPS:

Use FixedPointList?

Posted 16 days ago
185 Views
|
2 Replies
|
5 Total Likes
|

This is my code:

f[n_Integer] := If[ EvenQ[n] , f[n/2] , f[3 n + 1] ]

f[1] := 1

FixedPointList[f,6]

FixedPointList should give me {6,3,10,5,16,8,4,2,1,1}, but instead I'm only getting {6,1,1}

2 Replies

You are doing a double recursion here: your function is recursive and FixedPointList itself generates recursion. Here is what you want, I believe, for your Collatz Problem. Notice that you better specify a non-standard SameTest because otherwise you are going to get into an infinite loop as the values cycle over 4, 2, 1. I also stuck in the number 1000 to protect against the program going wild.

 f[n_Integer] := If[EvenQ[n], n/2, 3 n + 1]

 FixedPointList[f, 6, 1000, SameTest -> (#2 == 1 &)]
Posted 15 days ago

You are doing a double recursion here: your function is recursive and FixedPointList itself generates recursion.

thank you this was very helpful!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract