Skip to contents

Calculates the values that would return the input under the Collatz function.

Usage

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

Arguments

n

(numeric|bigz) The value on which to perform the reverse Collatz 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 list of either numeric or bigz type

Details

Returns the output of a single application of a Collatz-esque reverse function. If only one value is returned, it is the value that would be divided by P. If two values are returned, the first is the value that would be divided by P, and the second value is that which would undergo the multiply and add step, regardless of which is larger.

Examples

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