collatz(7) # n -> 3n+1
## [1] 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
collatz(9, short = TRUE)
## [1] 9 14 7 11 17 26 13 20 10 5 8 4 2 1
collatz(7, l = -1) # n -> 3n-1
## Found a non-trivial cycle for n = 7 !
## [1] 7 20 10 5 14 7
if (FALSE) {
collatz(5, k = 7, l = 1) # n -> 7n+1
## [1] 5 36 18 9 64 32 16 8 4 2 1
collatz(5, k = 7, l = -1) # n -> 7n-1
## Info: 5 --> 1.26995e+16 too big after 280 steps.
## Error in collatz(5, k = 7, l = -1) :
## Integer overflow, i.e. greater than 2^53-1
}
Run the code above in your browser using DataLab