Function Repository Resource:

# Collatz

Get the Collatz sequence starting with a given value

Contributed by: Enrique Zeleny
 ResourceFunction["Collatz"][n] gives the Collatz sequence starting with n. ResourceFunction["Collatz"][n,m] gives the first m iterations.

## Details

The Collatz sequence is obtained by starting from a positive integer and applying repeatedly the following operations: if the number is even, divide it by two, and if the number is odd, triple it and add one.
The Collatz conjecture states that this sequence always terminates in 1.

## Examples

### Basic Examples (2)

Collatz sequence for 13:

 In[1]:=
 Out[1]=

Show just the first 30 iterates for 27:

 In[2]:=
 Out[2]=

### Applications (4)

Highlight odd values:

 In[3]:=
 Out[3]=

Plot all the iterates for 27:

 In[4]:=
 Out[4]=

Plot the number of iterates needed for the first 100 integers:

 In[5]:=
 Out[5]=

Number of iterates needed for the first 2000 integers:

 In[6]:=
 Out[6]=

Paths of the iterates for the first 100 integers:

 In[7]:=
 Out[7]=

### Neat Examples (2)

Graph of the iterates for the first 26 integers:

 In[8]:=
 Out[8]=

Graph of the iterates for 500:

 In[9]:=
 Out[9]=

## Requirements

Wolfram Language 11.3 (March 2018) or above

## Version History

• 1.0.1 – 13 September 2021
• 1.0.0 – 12 February 2019