Documentation ¶
Index ¶
- func Add(n1, n2 []int, base int) ([]int, error)
- func BaseToDec(n []int, base int) (int, error)
- func DecToBase(n, base int) ([]int, error)
- func GreatestCommonDivisor(a, b int) int
- func IsValidNumber(n []int, base int) bool
- func NewErrInvalidBase() error
- func PrimeFactorization(inputNumber int) (map[int]int, error)
- func Subtract(n1, n2 []int, base int) ([]int, error)
- type ErrInvalidBase
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶ added in v0.2.18
Add adds two numbers of the same base. Both numbers are Least Significant Digit (LSD) first.
Parameters:
- n1: The first number to add.
- n2: The second number to add.
- base: The base of the numbers.
Returns:
- []int: The sum of the two numbers.
- error: An error if the base is invalid.
func BaseToDec ¶ added in v0.2.18
BaseToDec converts a number of the given base to a decimal number. The number's Least Significant Digit (LSD) is at index 0.
Parameters:
- n: The number to convert.
- base: The base of the number.
Returns:
- int: The decimal number.
- error: An error if the base is invalid or the number is invalid for the given base.
func DecToBase ¶ added in v0.2.18
DecToBase converts a decimal number to a number of the given base. The number's Least Significant Digit (LSD) is at index 0.
Parameters:
- n: The decimal number to convert.
- base: The base of the result number.
Returns:
- []int: The number in the given base.
- error: An error if the base is invalid.
func GreatestCommonDivisor ¶
GreatestCommonDivisor is a function that calculates the greatest common divisor (GCD) of two integers using the Euclidean algorithm.
Parameters:
- a, b: The two integers to find the GCD of.
Returns:
- int: The GCD of the two input numbers.
func IsValidNumber ¶ added in v0.2.18
IsValidNumber checks if the given number is valid for the given base.
Parameters:
- n: The number to check.
- base: The base of the number.
Returns:
- bool: True if the number is valid for the given base, false otherwise.
func NewErrInvalidBase ¶ added in v0.2.18
func NewErrInvalidBase() error
NewErrInvalidBase creates a new ErrInvalidBase error.
Returns:
- error: A new ErrInvalidBase error.
func PrimeFactorization ¶
PrimeFactorization is a function that performs prime factorization on an input number.
Panics with an error of type *ErrInvalidParameter if the input number is 0.
In the resulting map, no prime factor will have a value of 1, except for 1 and -1, which is represented as [1: 1]. Finally, negative numbers are converted to positive numbers.
Parameters:
- inputNumber: The number to factorize.
Returns:
- map[int]int: A map where the keys are the prime factors and the values are their respective powers.
func Subtract ¶ added in v0.2.18
Subtract subtracts two numbers of the same base. Both numbers are Least Significant Digit (LSD) first.
Parameters:
- n1: The number to subtract from.
- n2: The number to subtract.
- base: The base of the numbers.
Returns:
- []int: The result of the subtraction.
- error: An error if the base is invalid or the subtraction resulted in a negative number.
Types ¶
type ErrInvalidBase ¶ added in v0.2.18
type ErrInvalidBase struct{}
ErrInvalidBase is an error that is returned when the base is less than 1.
func (*ErrInvalidBase) Error ¶ added in v0.2.18
func (e *ErrInvalidBase) Error() string
Error is a method of ErrInvalidBase that returns the error message.
Returns:
- string: The error message.