Package io.github.skenvy
Class Collatz
- java.lang.Object
-
- io.github.skenvy.Collatz
-
public final class Collatz extends Object
Provides the basic functionality to interact with the Collatz conjecture. The parameterisation uses the same (P,a,b) notation as Conway's generalisations. Besides the function and reverse function, there is also functionality to retrieve the hailstone sequence, the "stopping time"/"total stopping time", or tree-graph.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
Collatz.FailedSaneParameterCheck
Thrown when eitherP
, the modulus, ora
, the multiplicand, are zero.static class
Collatz.HailstoneSequence
Contains the results of computing a hailstone sequence viaCollatz.hailstoneSequence(~)
.protected static class
Collatz.SaneParameterErrMsg
Error message constants, to be used as input to the FailedSaneParameterCheck.protected static class
Collatz.SequenceState
SequenceState for Cycle Control: Descriptive flags to indicate when some event occurs in the hailstone sequences or tree graph reversal, when set to verbose, or stopping time check.static class
Collatz.TreeGraph
Contains the results of computing the Tree Graph viaCollatz.treeGraph(~)
.static class
Collatz.TreeGraphNode
Nodes that form a "tree graph", structured as a tree, with their own node's value, as well as references to either possible child node, where a node can only ever have two children, as there are only ever two reverse values.
-
Field Summary
Fields Modifier and Type Field Description static BigInteger
DEFAULT_A
Default value fora
, the input's multiplicand.static BigInteger
DEFAULT_B
Default value forb
, the value added to the multiplied value.static BigInteger
DEFAULT_P
Default value forP
, the modulus condition.static BigInteger[][]
KNOWN_CYCLES
The four known cycles for the standard parameterisation.static BigInteger
VERIFIED_MAXIMUM
The current value up to which the standard parameterisation has been verified.static BigInteger
VERIFIED_MINIMUM
The current value down to which the standard parameterisation has been verified.
-
Constructor Summary
Constructors Constructor Description Collatz()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static BigInteger
function(BigInteger n)
Returns the output of a single application of the Collatz function.static BigInteger
function(BigInteger n, BigInteger p, BigInteger a, BigInteger b)
Returns the output of a single application of a Collatz-esque function.static Collatz.HailstoneSequence
hailstoneSequence(BigInteger initialValue, int maxTotalStoppingTime)
Returns a list of successive values obtained by iterating the Collatz function, until either 1 is reached, or the total amount of iterations exceeds maxTotalStoppingTime.static Collatz.HailstoneSequence
hailstoneSequence(BigInteger initialValue, BigInteger p, BigInteger a, BigInteger b, int maxTotalStoppingTime, boolean totalStoppingTime)
Returns a list of successive values obtained by iterating a Collatz-esque function, until either 1 is reached, or the total amount of iterations exceeds maxTotalStoppingTime, unless totalStoppingTime is False, which will terminate the hailstone at the "stopping time" value, i.e.static BigInteger[]
reverseFunction(BigInteger n)
Returns the output of a single application of the Collatz reverse function.static BigInteger[]
reverseFunction(BigInteger n, BigInteger p, BigInteger a, BigInteger b)
Returns the output of a single application of a Collatz-esque reverse function.static Double
stoppingTime(BigInteger initialValue)
Returns the stopping time, the amount of iterations required to reach a value less than the initial value, or null if maxStoppingTime is exceeded.static Double
stoppingTime(BigInteger initialValue, BigInteger p, BigInteger a, BigInteger b, int maxStoppingTime, boolean totalStoppingTime)
Returns the stopping time, the amount of iterations required to reach a value less than the initial value, or null if maxStoppingTime is exceeded.static Collatz.TreeGraph
treeGraph(BigInteger initialValue, int maxOrbitDistance)
Returns a directed tree graph of the reverse function values up to a maximum nesting of maxOrbitDistance, with the initialValue as the root.static Collatz.TreeGraph
treeGraph(BigInteger initialValue, int maxOrbitDistance, BigInteger p, BigInteger a, BigInteger b)
Returns a directed tree graph of the reverse function values up to a maximum nesting of maxOrbitDistance, with the initialValue as the root.
-
-
-
Field Detail
-
DEFAULT_P
public static final BigInteger DEFAULT_P
Default value forP
, the modulus condition.
-
DEFAULT_A
public static final BigInteger DEFAULT_A
Default value fora
, the input's multiplicand.
-
DEFAULT_B
public static final BigInteger DEFAULT_B
Default value forb
, the value added to the multiplied value.
-
KNOWN_CYCLES
public static final BigInteger[][] KNOWN_CYCLES
The four known cycles for the standard parameterisation.
-
VERIFIED_MAXIMUM
public static final BigInteger VERIFIED_MAXIMUM
The current value up to which the standard parameterisation has been verified.
-
VERIFIED_MINIMUM
public static final BigInteger VERIFIED_MINIMUM
The current value down to which the standard parameterisation has been verified.
-
-
Method Detail
-
function
public static BigInteger function(BigInteger n, BigInteger p, BigInteger a, BigInteger b) throws Collatz.FailedSaneParameterCheck
Returns the output of a single application of a Collatz-esque function.- Parameters:
n
- The value on which to perform the Collatz-esque function.p
- Modulus used to devide n, iff n is equivalent to (0 mod P).a
- Factor by which to multiply n.b
- Value to add to the scaled value of n.- Returns:
- The result of the function
- Throws:
Collatz.FailedSaneParameterCheck
- if P or a are 0.
-
function
public static BigInteger function(BigInteger n) throws Collatz.FailedSaneParameterCheck
Returns the output of a single application of the Collatz function.- Parameters:
n
- The value on which to perform the Collatz function.- Returns:
- The result of the function
- Throws:
Collatz.FailedSaneParameterCheck
- if P or a are 0.
-
reverseFunction
public static BigInteger[] reverseFunction(BigInteger n, BigInteger p, BigInteger a, BigInteger b) throws Collatz.FailedSaneParameterCheck
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.- Parameters:
n
- The value on which to perform the reverse Collatz function.p
- Modulus used to devide n, iff n is equivalent to (0 mod P).a
- Factor by which to multiply n.b
- Value to add to the scaled value of n.- Returns:
- The result of the function's reverse
- Throws:
Collatz.FailedSaneParameterCheck
- if P or a are 0.
-
reverseFunction
public static BigInteger[] reverseFunction(BigInteger n) throws Collatz.FailedSaneParameterCheck
Returns the output of a single application of the Collatz reverse function. If only one value is returned, it is the value that would be divided by 2. If two values are returned, the first is the value that would be divided by 2, and the second value is that which would undergo the 3N+1 step, regardless of which is larger.- Parameters:
n
- The value on which to perform the reverse Collatz function.- Returns:
- The result of the function's reverse
- Throws:
Collatz.FailedSaneParameterCheck
- if P or a are 0.
-
hailstoneSequence
public static Collatz.HailstoneSequence hailstoneSequence(BigInteger initialValue, BigInteger p, BigInteger a, BigInteger b, int maxTotalStoppingTime, boolean totalStoppingTime) throws Collatz.FailedSaneParameterCheck
Returns a list of successive values obtained by iterating a Collatz-esque function, until either 1 is reached, or the total amount of iterations exceeds maxTotalStoppingTime, unless totalStoppingTime is False, which will terminate the hailstone at the "stopping time" value, i.e. the first value less than the initial value. While the sequence has the capability to determine that it has encountered a cycle, the cycle from "1" wont be attempted or reported as part of a cycle, regardless of default or custom parameterisation, as "1" is considered a "total stop".- Parameters:
initialValue
- The value to begin the hailstone sequence from.p
- Modulus used to devide n, iff n is equivalent to (0 mod P).a
- Factor by which to multiply n.b
- Value to add to the scaled value of n.maxTotalStoppingTime
- Maximum amount of times to iterate the function, if 1 is not reached.totalStoppingTime
- Whether or not to execute until the "total" stopping time (number of iterations to obtain 1) rather than the regular stopping time (number of iterations to reach a value less than the initial value).- Returns:
- A set of values that form the hailstone sequence.
- Throws:
Collatz.FailedSaneParameterCheck
- if P or a are 0.
-
hailstoneSequence
public static Collatz.HailstoneSequence hailstoneSequence(BigInteger initialValue, int maxTotalStoppingTime) throws Collatz.FailedSaneParameterCheck
Returns a list of successive values obtained by iterating the Collatz function, until either 1 is reached, or the total amount of iterations exceeds maxTotalStoppingTime.- Parameters:
initialValue
- The value to begin the hailstone sequence from.maxTotalStoppingTime
- Maximum amount of times to iterate the function, if 1 is not reached.- Returns:
- A set of values that form the hailstone sequence.
- Throws:
Collatz.FailedSaneParameterCheck
- if P or a are 0.
-
stoppingTime
public static Double stoppingTime(BigInteger initialValue, BigInteger p, BigInteger a, BigInteger b, int maxStoppingTime, boolean totalStoppingTime) throws Collatz.FailedSaneParameterCheck
Returns the stopping time, the amount of iterations required to reach a value less than the initial value, or null if maxStoppingTime is exceeded. Alternatively, if totalStoppingTime is True, then it will instead count the amount of iterations to reach 1. If the sequence does not stop, but instead ends in a cycle, the result will be (Double.POSITIVE_INFINITY). If (P,a,b) are such that it is possible to get stuck on zero, the result will be the negative of what would otherwise be the "total stopping time" to reach 1, where 0 is considered a "total stop" that should not occur as it does form a cycle of length 1.- Parameters:
initialValue
- The value for which to find the stopping time.p
- Modulus used to devide n, iff n is equivalent to (0 mod P).a
- Factor by which to multiply n.b
- Value to add to the scaled value of n.maxStoppingTime
- Maximum amount of times to iterate the function, if the stopping time is not reached. IF the maxStoppingTime is reached, the function will return null.totalStoppingTime
- (bool): Whether or not to execute until the "total" stopping time (number of iterations to obtain 1) rather than the regular stopping time (number of iterations to reach a value less than the initial value).- Returns:
- The stopping time, or, in a special case, infinity, null or a negative.
- Throws:
Collatz.FailedSaneParameterCheck
- if P or a are 0.
-
stoppingTime
public static Double stoppingTime(BigInteger initialValue) throws Collatz.FailedSaneParameterCheck
Returns the stopping time, the amount of iterations required to reach a value less than the initial value, or null if maxStoppingTime is exceeded. Alternatively, if totalStoppingTime is True, then it will instead count the amount of iterations to reach 1. If the sequence does not stop, but instead ends in a cycle, the result will be (Double.POSITIVE_INFINITY).- Parameters:
initialValue
- The value for which to find the stopping time.- Returns:
- The stopping time, or, in a cycle case, infinity.
- Throws:
Collatz.FailedSaneParameterCheck
- if P or a are 0.
-
treeGraph
public static Collatz.TreeGraph treeGraph(BigInteger initialValue, int maxOrbitDistance, BigInteger p, BigInteger a, BigInteger b) throws Collatz.FailedSaneParameterCheck
Returns a directed tree graph of the reverse function values up to a maximum nesting of maxOrbitDistance, with the initialValue as the root.- Parameters:
initialValue
- The root value of the directed tree graph.maxOrbitDistance
- Maximum amount of times to iterate the reverse function. There is no natural termination to populating the tree graph, equivalent to the termination of hailstone sequences or stopping time attempts, so this is not an optional argument like maxStoppingTime / maxTotalStoppingTime, as it is the intended target of orbits to obtain, rather than a limit to avoid uncapped computation.p
- Modulus used to devide n, iff n is equivalent to (0 mod P).a
- Factor by which to multiply n.b
- Value to add to the scaled value of n.- Returns:
- the entire tree graph up to some orbit distance, for the given parameters.
- Throws:
Collatz.FailedSaneParameterCheck
- if P or a are 0.
-
treeGraph
public static Collatz.TreeGraph treeGraph(BigInteger initialValue, int maxOrbitDistance) throws Collatz.FailedSaneParameterCheck
Returns a directed tree graph of the reverse function values up to a maximum nesting of maxOrbitDistance, with the initialValue as the root.- Parameters:
initialValue
- The root value of the directed tree graph.maxOrbitDistance
- Maximum amount of times to iterate the reverse function. There is no natural termination to populating the tree graph, equivalent to the termination of hailstone sequences or stopping time attempts, so this is not an optional argument like maxStoppingTime / maxTotalStoppingTime, as it is the intended target of orbits to obtain, rather than a limit to avoid uncapped computation.- Returns:
- the entire tree graph up to some orbit distance, for the given parameters.
- Throws:
Collatz.FailedSaneParameterCheck
- if P or a are 0.
-
-