login
a(0) = 2; a(n) = a(n - 1) * 3 + 1 if a(n - 1) is prime, or a(n - 1) / (smallest prime factor) if it is composite.
2

%I #9 Aug 05 2023 12:01:28

%S 2,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,7,22,11,34,17,52,26,13,

%T 40,20,10,5,16,8,4,2,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,7,22,

%U 11,34,17,52,26,13,40,20,10,5,16,8,4,2,7,22,11,34,17,52,26,13,40,20,10,5,16,8

%N a(0) = 2; a(n) = a(n - 1) * 3 + 1 if a(n - 1) is prime, or a(n - 1) / (smallest prime factor) if it is composite.

%C a(n) repeats itself after 16 iterations. Peak is a(6) = 52.

%C The function is similar in nature to Collatz's 3x+1 problem, except that it deals with primality instead of parity.

%H Wikipedia, <a href="http://en.wikipedia.org/en/wiki/Divisor">Divisor</a>

%H Wikipedia, <a href="http://en.wikipedia.org/en/wiki/Prime_number">Prime number</a>

%e a(0) = 2

%e a(1) = 2 * 3 + 1 = 7, because a(0) was prime.

%e a(2) = 7 * 3 + 1 = 22, because a(1) was prime.

%e a(3) = 22 / 2 = 11, because the smallest prime factor of a(2) was 2.

%t NestList[If[PrimeQ[#],3#+1,#/FactorInteger[#][[1,1]]]&,2,80] (* or *) PadRight[{},80,{2,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4}] (* _Harvey P. Dale_, Aug 05 2023 *)

%o (Python)

%o import math

%o from sympy import isprime

%o a = [2]

%o while not a[ -1] in a[:-1]:

%o if isprime(a[ -1]):

%o a.append(a[ -1] * 3 + 1)

%o else:

%o for div in range(2, int(math.sqrt(a[ -1])) + 1):

%o if not a[ -1] % div:

%o a.append(a[ -1] // div)

%o break

%o print(a)

%Y Cf. A000040, A175867.

%K easy,nonn

%O 0,1

%A _Grant Garcia_, Oct 02 2010