OFFSET
0,1
LINKS
FORMULA
From Colin Barker, Oct 04 2019: (Start)
G.f.: (9 + 28*x + 14*x^2 - 2*x^3 - 6*x^4 - 3*x^5 + 27*x^6 - 5*x^7 + 41*x^8 - 8*x^9 - 4*x^10 - 12*x^11 - 6*x^12 - 3*x^13 - 35*x^14 - 4*x^15 - 2*x^16 - x^17 - 14*x^18 - 7*x^19) / ((1 - x)*(1 + x + x^2)).
a(n) = a(n-3) for n>19.
(End)
EXAMPLE
9 is odd, so the next term is 3*9 + 1 = 28.
28 is even, so the next term is 28/2 = 14.
MATHEMATICA
NestList[If[EvenQ[#], #/2, 3# + 1] &, 9, 100] (* Harvey P. Dale, Dec 16 2012 *)
PROG
(Scala) def collatz(n: Int): Int = (n % 2) match {
case 0 => n / 2
case 1 => 3 * n + 1
}
import scala.collection.mutable.ListBuffer
val start = 9; var curr = start; var trajectory = new ListBuffer[Int]()
for (_ <- 1 to 100) {
trajectory += curr; curr = collatz(curr)
}
trajectory // Alonso del Arte, Jun 02 2019
(PARI) Vec((9 + 28*x + 14*x^2 - 2*x^3 - 6*x^4 - 3*x^5 + 27*x^6 - 5*x^7 + 41*x^8 - 8*x^9 - 4*x^10 - 12*x^11 - 6*x^12 - 3*x^13 - 35*x^14 - 4*x^15 - 2*x^16 - x^17 - 14*x^18 - 7*x^19) / ((1 - x)*(1 + x + x^2)) + O(x^80)) \\ Colin Barker, Oct 04 2019
(Python)
from itertools import accumulate
def f(x, _): return x//2 if x%2 == 0 else 3*x+1
print(list(accumulate([9]*92, f))) # Michael S. Branicky, Sep 28 2021
CROSSREFS
KEYWORD
nonn,easy
AUTHOR
STATUS
approved