Message Boards Message Boards

0
|
4557 Views
|
2 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Use FixedPointList?

Posted 5 years ago

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}

POSTED BY: Mellow hibey
2 Replies
Posted 5 years ago

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

thank you this was very helpful!

POSTED BY: Mellow hibey

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 BY: Seth Chandler
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