Documentation ¶
Overview ¶
Package gomath contains nifty math routines written in go
Index ¶
- func Factors(n int64) []int64
- func Inverse(f func(float64) float64, y float64, lower float64, upper float64) float64
- func Jagged(x float64) float64
- func ProductsSlice(n, count int) []int64
- type BigIntCounter
- type BigIntStream
- type Cake
- type IntCounter
- type IntStream
- type NthBigInt
- type NthInt
- type Partition
- type Point
- type PrimePower
- type Spline
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Factors ¶ added in v1.2.0
Factors returns all the positive integers that divide n in ascending order. Factors panics if n < 1.
func Inverse ¶
Inverse returns x such that f(x) = y. Caller must choose lower and upper so that x falls in between them. f must be monotone increasing or decreasing between lower and upper.
func Jagged ¶ added in v1.3.0
Jagged is a function that is continuous everywhere but differentiable nowhere.
func ProductsSlice ¶ added in v1.2.0
ProductsSlice returns all the numbers in ascending order that can be written as a product of count positive integers each ranging between 1 and n. ProductsSlice panics if n < 1 or if count is negative.
Types ¶
type BigIntCounter ¶
type BigIntCounter struct {
// contains filtered or unexported fields
}
BigIntCounter counts how many values on wrapped BigIntStream are less than or equal to a given value. Wrapped stream must be monotone increasing.
func NewBigIntCounter ¶
func NewBigIntCounter(stream BigIntStream) *BigIntCounter
NewBigIntCounter returns a BigIntCounter that wraps stream.
type BigIntStream ¶
type BigIntStream interface { // Next stores the next big.Int value at value and returns value. Next(value *big.Int) *big.Int }
BigIntStream represents an infinite stream of big.Int values.
func BigIntSummation ¶
func BigIntSummation(stream BigIntStream) BigIntStream
BigIntSummation generates the summation of the values in stream. For example if stream generates the primes, BigIntSummation would generate 2, 5, 10, 17, ...
func Fibonacci ¶
func Fibonacci(first, second int64) BigIntStream
Fibonacci generates fibonacci numbers. first and second are the first and second terms in the sequence, normally 1 and 1.
func Partitions ¶
func Partitions() BigIntStream
Partitions generates p(1), p(2), p(3), ... where p is the partition function. See Partition.
func Ugly ¶
func Ugly(primeFactors ...int64) BigIntStream
Ugly returns the numbers whose prime factors are a subset of primeFactors in ascending order.
type Cake ¶
type Cake struct {
// contains filtered or unexported fields
}
Cake is used to compute the number of pieces you can get cutting an n dimensional cake k times. Cake instances are not safe to use with multiple goroutines
type IntCounter ¶
type IntCounter struct {
// contains filtered or unexported fields
}
IntCounter counts how many values on wrapped IntStream are less than or equal to a given value. Wrapped stream must be monotone increasing.
func NewIntCounter ¶
func NewIntCounter(stream IntStream) *IntCounter
NewIntCounter returns an IntCounter that wraps stream.
func (*IntCounter) CountLE ¶
func (i *IntCounter) CountLE(value int64) int64
CountLE returns how many values on wrapped stream are less than or equal to value. CountLE panics if value is less than value in the previous call to CountLE.
type IntStream ¶
type IntStream interface { // Next returns the next int64 value. If there are no more values, Next // returns 0, false. Next() (int64, bool) }
IntStream represents a finite stream of int64 values.
func DecadePrimes ¶
DecadePrimes generates all x >= start in ascending order such that 10x + 1, 10x + 3, 10x + 7, and 10x + 9 are all prime. DecadePrimes stops generating when 10*x > math.MaxInt64. If start*10 > math.MaxInt64, DecadePrimes generates nothing.
func Happys ¶
Happys generates all the happy numbers in order that are greater than or equal to start.
func Harshads ¶
Harshads generates the harshad numbers in order that are greater than or equal to start.
func IntSummation ¶
IntSummation generates the summation of the values in stream. For example if stream generates the primes, IntSummation would generate 2, 5, 10, 17, ...
type NthBigInt ¶
type NthBigInt struct {
// contains filtered or unexported fields
}
NthBigInt wraps a BigIntStream and provides the Nth method to return the Nth big.Int off the stream.
func NewNthBigInt ¶
func NewNthBigInt(stream BigIntStream) *NthBigInt
NewNthBigInt creates a new instance that wraps stream.
func (*NthBigInt) Nth ¶
Nth returns the nth big.Int taken from the wrapped stream. n is 1-based so 1 returns the first big.Int, 2 returns the second etc. The nth big.Int is stored at value, and Nth returns this value. Nth panics if n is not greater than the number of values already taken from the stream.
type NthInt ¶
type NthInt struct {
// contains filtered or unexported fields
}
NthInt wraps an IntStream and provides the Nth method to return the Nth int64 off the stream.
type Partition ¶
type Partition struct {
// contains filtered or unexported fields
}
Partition computes the partition function, p, which calculates how many ways n can be partitioned when order doesn't matter.
For example: p(4) = 5 because there are 5 ways to express 4 as a sum when order doesn't matter. 4 = 1+1+1+1, 4 = 1+1+2 (covers 2+1+1 and 1+2+1), 4 = 1+3 (covers 3+1), 4 = 2+2, 4 = 4
Partition instances are not safe to use with multiple goroutines.
type PrimePower ¶
PrimePower represents a single term of a prime power decomposition
func Factor ¶
func Factor(n int64) []PrimePower
Factor returns the prime power decomposition of n. Factor panics if n < 1.
type Spline ¶
type Spline struct {
// contains filtered or unexported fields
}
Spline represents a cubic spline.
func NewSpline ¶
NewSpline returns a new cubic spline going through each point in points. The second derivative of the spline at the first and last point is 0. The x values in points must be strictly increasing.
func NewSplineWithSlopes ¶
NewSplineWithSlopes returns a new cubic spline going through each point in points. The x values in points must be strictly increasing. beginSlope and endSlope specify the slope of the spline at the first point and last point respectively.
func (*Spline) Eval ¶
Eval evaluates this cubic spline at x. Eval panics if x doesn't fall between what MinX and MaxX return. Eval also panics if called on the zero value Spline.