math

package
v1.1.28 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArithmeticSeries

func ArithmeticSeries[T constraints.Integer](start, diff, terms T) T

ArithmeticSeries calculates the sum of an arithmetic sequence in O(1) time because the formula n/2 * (2a + (n-1)d) has existed since forever

func ArithmeticSeriesSlow

func ArithmeticSeriesSlow[T constraints.Integer](start, diff, terms T) T

ArithmeticSeriesSlow calculates the sum of an arithmetic sequence with a loop for those who really enjoy repetitive tasks

func Binomial

func Binomial[T constraints.Signed](n, k T) (T, error)

Binomial calculates C(n, k) in O(k) without computing factorials directly. It's "faster" and less prone to immediate overflow than the naive approach but let's not pretend it won't blow up eventually for big n.

func BinomialSlow

func BinomialSlow[T constraints.Signed](n, k T) (T, error)

BinomialSlow calculates C(n, k) = n! / (k!(n-k)!) in the most naive way guaranteed to overflow for large n, just like your inbox

func Factorial

func Factorial[T constraints.Signed](n T) (T, error)

Factorial tries to do factorial "faster", but let's be honest, there's no real O(1) direct formula for factorial that gives exact integers. We'll just do a loop with an overflow check, so you can pretend you're being safe with large numbers.

func FactorialSlow

func FactorialSlow[T constraints.Integer](n T) T

FactorialSlow calculates n! in the most straightforward way by multiplying from 1 up to n in O(n) time because why do in O(1) what you can do in O(n)?

func FactorialStirlingApprox

func FactorialStirlingApprox[T constraints.Integer](n T) float64

FactorialStirlingApprox returns a float64 approximation of n! using Stirling's approximation: sqrt(2πn) * (n/e)^n because sometimes "close enough" is good enough for government work

func Fibonacci

func Fibonacci[T constraints.Integer](n T) T

Fibonacci calculates the nth Fibonacci number using fast doubling in O(log n) time because life is too short for naive recursion

func FibonacciSlow

func FibonacciSlow[T constraints.Integer](n T) T

FibonacciSlow calculates the nth Fibonacci number recursively in O(1.618^n) time, or something equally terrifying because we all love the idea of a stack overflow

func GeometricSeries

func GeometricSeries[T constraints.Integer](start, ratio, terms T) T

GeometricSeries calculates the sum of a geometric series in O(log n) time ignoring the cost of exponentiation just for your mental comfort sum = a * (r^n - 1) / (r - 1), if r != 1

func GeometricSeriesSlow

func GeometricSeriesSlow[T constraints.Integer](start, ratio, terms T) T

GeometricSeriesSlow calculates the sum of a geometric series with a loop because sometimes you just want to multiply the same thing over and over again

func MustSum

func MustSum[T constraints.Signed](n T) T

MustSum is like SumWithOverflowCheck but panics on error because some days you just want to watch the world burn

func Pow

func Pow[T constraints.Integer](base, exp T) T

Pow computes base^exp using fast exponentiation in O(log exp) time because naive exponentiation is so last century

func Sum

func Sum[T constraints.Integer](n T) T

Sum calculates the sum of numbers from 1 to n (or n to 1 if n is negative) in O(1) time because who needs loops when you have math from 300 BC? Gauss would be proud, or maybe just mildly amused.

func SumOfCubes

func SumOfCubes[T constraints.Integer](n T) T

SumOfCubes calculates the sum of cubes from 1 to n using the formula [n(n+1)/2]^2 in O(1) time because squares weren't enough to show off our math prowess

func SumOfCubesSlow

func SumOfCubesSlow[T constraints.Integer](n T) T

SumOfCubesSlow calculates the sum of cubes from 1 to n using a loop let's waste even more CPU cycles just because we can

func SumOfFourthPowers

func SumOfFourthPowers[T constraints.Integer](n T) T

SumOfFourthPowers calculates the sum of the fourth powers from 1 to n using the formula n(n+1)(2n+1)(3n^2+3n-1)/30 in O(1) time because there's always a bigger power to inflate your ego

func SumOfFourthPowersSlow

func SumOfFourthPowersSlow[T constraints.Integer](n T) T

SumOfFourthPowersSlow calculates the sum of the fourth powers from 1 to n using a loop because apparently squares and cubes are just too basic for some people

func SumOfSquares

func SumOfSquares[T constraints.Integer](n T) T

SumOfSquares calculates the sum of squares from 1 to n using the formula n(n+1)(2n+1)/6 in O(1) time because there's always a formula if you know where to look

func SumOfSquaresSlow

func SumOfSquaresSlow[T constraints.Integer](n T) T

SumOfSquaresSlow calculates the sum of squares from 1 to n using a loop because some of us like burning CPU cycles for no good reason obviously this should not be used, it's just here for the memes.

func SumRange

func SumRange[T constraints.Integer](start, end T) T

SumRange calculates the sum of numbers from start to end inclusive because sometimes you don't want to start at 1 like a peasant

func SumRangeSlow

func SumRangeSlow[T constraints.Integer](start, end T) T

SumRangeSlow calculates the sum of numbers from start to end inclusive because some of us like burning CPU cycles for no good reason obviously this should not be used, it's just here for the memes.

func SumSlow

func SumSlow[T constraints.Integer](n T) T

SumSlow calculates the sum of numbers from 1 to n using a loop because some of us like burning CPU cycles for no good reason obviously this should not be used, it's just here for the memes.

func SumWithOverflowCheck

func SumWithOverflowCheck[T constraints.Signed](n T) (T, error)

SumWithOverflowCheck does the same as Sum but checks for overflow because some people actually care about correctness

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL