Skip to contents

Returns the output of a single application of a Collatz-esque function.

Usage

collatz_function(n, P = 2, a = 3, b = 1)

Arguments

n

(numeric|bigz) The value on which to perform the Collatz-esque function

P

(numeric|bigz): Modulus used to divide n, iff n is equivalent to (0 mod P). Default is 2.

a

(numeric|bigz) Factor by which to multiply n. Default is 3.

b

(numeric|bigz) Value to add to the scaled value of n. Default is 1.

Value

a numeric, either in-built or a bigz from the gmp library.

Details

This function will compute and return the result of applying one iteration of a parameterised Collatz-esque function. Although it will operate with integer inputs, for overflow safety, provide a gmp Big Integer ('bigz').

Examples

# 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)
#> [1] 16
collatz_function(16)
#> [1] 8
# If we want change the default parameterisation we can;
collatz_function(4, 5, 2, 3)
#> [1] 11
# Or if we only want to change one of them
collatz_function(3, a=-2)
#> [1] -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;
collatz_function(99999999999999999999)
#> Warning: probable complete loss of accuracy in modulus
#> [1] 5e+19
collatz_function(as.bigz("99999999999999999999"))
#> Big Integer ('bigz') :
#> [1] 299999999999999999998