collatz In collatz: Functions Related to the Collatz/Syracuse/3n+1 Problem

```knitr::opts_chunk\$set(
collapse = TRUE,
comment = "#>"
)
```

Install

After following the "Getting Started" instructions (which can also be seen in the README) to download and or install the package, it can be imported with;

```library(collatz)
# Optionally
library(gmp)
```

You can jump right into using the functions in this package with integer inputs, or you can use `bigz` from the `gmp` library. Each function, amongst possible other parameters, comes with the default parameters `(P=2,a=3,b=1)` as optional inputs. In each case, `P[=2]` is the modulus to check for divisibility of the input by, `a[=3]` is the factor to multiply the input by, and `b[=1]` is the value to add to the product of `a[=3]` and the input. There are two basic commands to start with; the `collatz_function` and the `reverse_function`.

collatz_function

Returns the output of a single application of a Collatz-esque function. Without `gmp` or parameterisation, we can try something simple like

```collatz_function(5)
collatz_function(16)
```

If we want change the default parameterisation we can;

```collatz_function(4, 5, 2, 3)
```

Or if we only want to change one of them

```collatz_function(3, a=-2)
```

All the above work fine, but the function doesn't offer protection against overflowing integers by default. To venture into the world of arbitrary integer inputs we can use an `as.bigz` from `gmp`. Compare the two;

```collatz_function(99999999999999999999)
collatz_function(as.bigz("99999999999999999999"))
```

reverse_function

Calculates the values that would return the input under the Collatz function. Without `gmp` or parameterisation, we can try something simple like

```reverse_function(1)
reverse_function(2)
reverse_function(4)
```

If we want change the default parameterisation we can;

```reverse_function(3, -3, -2, -5)
```

Or if we only want to change one of them

```reverse_function(16, a=5)
```

All the above work fine, but the function doesn't offer protection against overflowing integers by default. To venture into the world of arbitrary integer inputs we can use an `as.bigz` from `gmp`. Compare the two;

```reverse_function(99999999999999999999)
reverse_function(as.bigz("99999999999999999999"))
```

stopping_time

Calculates the "stopping time", or optionally the "total" stopping time. Without `gmp` or parameterisation, we can try something simple like

```stopping_time(27)
stopping_time(27, total_stopping_time=TRUE)
```

If we want change the default parameterisation we can;

```stopping_time(3, 5, 2, 1)
```

Or if we only want to change one of them

```stopping_time(17, a=5)
```

All the above work fine, but the function doesn't offer protection against overflowing integers by default. To venture into the world of arbitrary integer inputs we can use an `as.bigz` from `gmp`. Compare the two;

```stopping_time(99999999999999999999)
stopping_time(as.bigz("99999999999999999999"))
```

As an extra note, the original motivation for creating a range of Collatz themed packages came from some earlier scripts for calculating the stopping distances under certain parameterisations. An inconsequential result of which was observing that all of the following, for however high `k` goes, should equal `96`!

```stopping_time(27)
stopping_time(27+as.bigz("576460752303423488"))
stopping_time(27+(2*as.bigz("576460752303423488")))
stopping_time(27+(3*as.bigz("576460752303423488")))
stopping_time(27+(4*as.bigz("576460752303423488")))
```

Try the collatz package in your browser

Any scripts or data that you put into this service are public.

collatz documentation built on Sept. 5, 2022, 9:06 a.m.