java.lang.Object
net.luis.utils.math.algorithm.ExtendedEuclideanAlgorithm
Implementation of the Extended Euclidean Algorithm (EEA).
The EEA is an extension of the Euclidean Algorithm and is used to find the greatest common divisor (GCD) of two integers.
The EEA is an extension of the Euclidean Algorithm and is used to find the greatest common divisor (GCD) of two integers.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate BigInteger
The current divisor of the EEA.private final List
<BigInteger> The first coefficients of the EEA.private final BigInteger
The initial divisor of the EEA.private final BigInteger
The initial value of the EEA.private BigInteger
The current quotient of the EEA.private BigInteger
The current remainder of the EEA.private final List
<BigInteger> The second coefficients of the EEA.private int
The number of steps the EEA has executed.private BigInteger
The current value of the EEA. -
Constructor Summary
ConstructorsConstructorDescriptionExtendedEuclideanAlgorithm
(int value, int divisor) Constructs a new EEA with the given value and divisor.ExtendedEuclideanAlgorithm
(long value, long divisor) Constructs a new EEA with the given value and divisor.ExtendedEuclideanAlgorithm
(@NotNull BigInteger value, @NotNull BigInteger divisor) Constructs a new EEA with the given value and divisor. -
Method Summary
Modifier and TypeMethodDescriptionboolean
void
execute()
Executes the EEA for a single step.void
execute
(int steps) Executes the EEA for the given number of steps.
If the EEA is already completed the method will return immediately.private void
Executes a single step of the EEA.
The values of the EEA are recalculated.void
Executes the EEA until it is complete.static int
gcd
(int value, int divisor) Calculates the greatest common divisor (GCD) of the given value and divisor using the EEA.static long
gcd
(long value, long divisor) Calculates the greatest common divisor (GCD) of the given value and divisor using the EEA.static @NotNull BigInteger
gcd
(@NotNull BigInteger value, @NotNull BigInteger divisor) Calculates the greatest common divisor (GCD) of the given value and divisor using the EEA.@NotNull BigInteger
Returns the current divisor of the EEA.@NotNull BigInteger
Returns the first coefficient of the EEA.private @NotNull BigInteger
getFirstCoefficient
(int step) Gets the first coefficient of the EEA.
The number of steps is subtracted from the size of the first coefficients list to get the correct coefficient.@NotNull BigInteger
Returns the initial divisor of the EEA.@NotNull BigInteger
Returns the initial value of the EEA.@NotNull BigInteger
Returns the current quotient of the EEA.@NotNull BigInteger
Returns the current remainder of the EEA.@NotNull BigInteger
Returns the second coefficient of the EEA.private @NotNull BigInteger
getSecondCoefficient
(int step) Gets the second coefficient of the EEA.
The number of steps is subtracted from the size of the second coefficients list to get the correct coefficient.int
getStep()
Returns the number of steps the EEA has executed.@NotNull BigInteger
getValue()
Returns the current value of the EEA.int
hashCode()
boolean
Checks if the EEA is complete.toString()
private void
update
(@NotNull BigInteger value, @NotNull BigInteger divisor, @NotNull BigInteger remainder, @NotNull BigInteger quotient) Updates the values of the EEA.
The first and second coefficients are calculated and added to their respective lists.
The value, divisor, remainder, quotient and step are updated.
-
Field Details
-
firstCoefficients
The first coefficients of the EEA. -
secondCoefficients
The second coefficients of the EEA. -
initialValue
The initial value of the EEA. -
initialDivisor
The initial divisor of the EEA. -
value
The current value of the EEA. -
divisor
The current divisor of the EEA. -
remainder
The current remainder of the EEA. -
quotient
The current quotient of the EEA. -
step
private int stepThe number of steps the EEA has executed.
-
-
Constructor Details
-
ExtendedEuclideanAlgorithm
public ExtendedEuclideanAlgorithm(int value, int divisor) Constructs a new EEA with the given value and divisor.- Parameters:
value
- The value of the EEAdivisor
- The divisor of the EEA- Throws:
IllegalArgumentException
- If the value or divisor is 0
-
ExtendedEuclideanAlgorithm
public ExtendedEuclideanAlgorithm(long value, long divisor) Constructs a new EEA with the given value and divisor.- Parameters:
value
- The value of the EEAdivisor
- The divisor of the EEA- Throws:
IllegalArgumentException
- If the value or divisor is 0
-
ExtendedEuclideanAlgorithm
public ExtendedEuclideanAlgorithm(@NotNull @NotNull BigInteger value, @NotNull @NotNull BigInteger divisor) Constructs a new EEA with the given value and divisor.- Parameters:
value
- The value of the EEAdivisor
- The divisor of the EEA- Throws:
IllegalArgumentException
- If the value or divisor is 0
-
-
Method Details
-
gcd
public static int gcd(int value, int divisor) Calculates the greatest common divisor (GCD) of the given value and divisor using the EEA.- Parameters:
value
- The value to calculate the GCD fordivisor
- The divisor to calculate the GCD for- Returns:
- The GCD of the given value and divisor
- Throws:
IllegalArgumentException
- If the value or divisor is 0
-
gcd
public static long gcd(long value, long divisor) Calculates the greatest common divisor (GCD) of the given value and divisor using the EEA.- Parameters:
value
- The value to calculate the GCD fordivisor
- The divisor to calculate the GCD for- Returns:
- The GCD of the given value and divisor
- Throws:
IllegalArgumentException
- If the value or divisor is 0
-
gcd
@NotNull public static @NotNull BigInteger gcd(@NotNull @NotNull BigInteger value, @NotNull @NotNull BigInteger divisor) Calculates the greatest common divisor (GCD) of the given value and divisor using the EEA.- Parameters:
value
- The value to calculate the GCD fordivisor
- The divisor to calculate the GCD for- Returns:
- The GCD of the given value and divisor
- Throws:
IllegalArgumentException
- If the value or divisor is 0
-
isComplete
public boolean isComplete()Checks if the EEA is complete.- Returns:
- True, if the EEA is complete, otherwise false
-
getInitialValue
Returns the initial value of the EEA.- Returns:
- The initial value
-
getInitialDivisor
Returns the initial divisor of the EEA.- Returns:
- The initial divisor
-
getValue
Returns the current value of the EEA.- Returns:
- The current value
-
getDivisor
Returns the current divisor of the EEA.- Returns:
- The current divisor
-
getRemainder
Returns the current remainder of the EEA.- Returns:
- The current remainder
-
getQuotient
Returns the current quotient of the EEA.- Returns:
- The current quotient
-
getStep
public int getStep()Returns the number of steps the EEA has executed.- Returns:
- The number of steps
-
getFirstCoefficient
Returns the first coefficient of the EEA.- Returns:
- The first coefficient
-
getSecondCoefficient
Returns the second coefficient of the EEA.- Returns:
- The second coefficient
-
execute
public void execute()Executes the EEA for a single step. -
execute
public void execute(int steps) Executes the EEA for the given number of steps.
If the EEA is already completed the method will return immediately.- Parameters:
steps
- The number of steps to execute
-
executeUntilComplete
public void executeUntilComplete()Executes the EEA until it is complete. -
getFirstCoefficient
Gets the first coefficient of the EEA.
The number of steps is subtracted from the size of the first coefficients list to get the correct coefficient.- Parameters:
step
- The number of steps to go back- Returns:
- The first coefficient of the EEA
-
getSecondCoefficient
Gets the second coefficient of the EEA.
The number of steps is subtracted from the size of the second coefficients list to get the correct coefficient.- Parameters:
step
- The number of steps to go back- Returns:
- The second coefficient of the EEA
-
executeStep
private void executeStep()Executes a single step of the EEA.
The values of the EEA are recalculated.- See Also:
-
update
private void update(@NotNull @NotNull BigInteger value, @NotNull @NotNull BigInteger divisor, @NotNull @NotNull BigInteger remainder, @NotNull @NotNull BigInteger quotient) Updates the values of the EEA.
The first and second coefficients are calculated and added to their respective lists.
The value, divisor, remainder, quotient and step are updated.- Parameters:
value
- The new valuedivisor
- The new divisorremainder
- The new remainderquotient
- The new quotient
-
equals
-
hashCode
public int hashCode() -
toString
-