maths

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: MIT Imports: 5 Imported by: 3

README

Maths Extension library

License Go Report Card codecov Build and test CodeQL Lint Code Base

codecov - svg

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(x int) int

Abs returns the |x|. Does not handle math.MinInt64.

func Binomial

func Binomial(n, k int) int

Binomial returns to binomial coefficient of (|n|, |k|). It does not handle int overflows when numbers get too large. Use big.Binomial() instead.

func Digits

func Digits(x int) <-chan int

Digits returns and fills a channel with the digits of x starting with the smallest magnitude numbers (right to left).

func DigitsBig

func DigitsBig(x *big.Int) <-chan int

DigitsBig fills and returns a channel with the digits of x starting with the smallest magnitude numbers (right to left).

func Factorial

func Factorial(n int) int

Factorial returns the factorial of |n|. It does not handle int overflows when numbers get too large. Use big.MulRange() instead.

func GCD

func GCD(numbers ...int) int

GCD returns the greatest common divisor of a group of integers. GCD() = 0 GCD(a, 0) = |a| Does not handle math.MinInt64

func GetDivisors added in v1.1.1

func GetDivisors(x int) <-chan int

GetDivisors fills a channel with all the (positive) divisors of x, unsorted. Uses PrimeFactorisation(x). Does not handle math.MinInt64.

func GetDivisorsBruteForce added in v1.1.1

func GetDivisorsBruteForce(x int) <-chan int

GetDivisorsBruteForce fills a channel with all the (positive) divisors of x, unsorted. Uses a brute force method.

func GetPrimeNumbers

func GetPrimeNumbers() (<-chan int, chan<- bool)

GetPrimeNumbers returns a channel from which to siphon off the prime numbers in order, as needed. Send a boolean to the Done channel when finished. The prime sieve: Daisy-chain Filter processes.

func GetPrimeNumbersBelow

func GetPrimeNumbersBelow(n int) <-chan int

GetPrimeNumbersBelow fills a channel with the prime numbers below |n|. Uses a euclidean sieve.

func LCM

func LCM(numbers ...int) int

LCM returns the least common multiple of a group of integers. This method uses GCD(). LCM() = 0 LCM(a, 0) = |a| Does not handle int overflows if the numbers get too large. Use LCMBig.

func LCMBig

func LCMBig(numbers ...*big.Int) *big.Int

LCMBig returns the least common multiple of a group of integers. This method uses (*Int) GCD(). LCMBig() = 0 LCMBig(a, 0) = |a|

func Max

func Max(numbers ...int) int

Max returns the maximum int from a group of integers. Max() = 0

func MaxPath

func MaxPath(t *Tree) int

MaxPath returns the largest of all the possible summations from top to bottom of a tree. The execution works up from the bottom of the pyramid. The maximum path to a node is the value of the node plus the maximum of the maximum paths to each child node. There is a natural recursive function but it fails when a pyramid tree gets too large and the function runs out of resources. MaxPath(<nil>) returns 0.

func NumberOfDigits

func NumberOfDigits(x int) int

NumberOfDigits returns the number of digits x has. Uses integer-string conversion.

func NumberOfDigitsBig

func NumberOfDigitsBig(x *big.Int) int

NumberOfDigitsBig returns the number of digits x has. Uses integer-string conversion.

func NumberOfDivisors

func NumberOfDivisors(x int) int

NumberOfDivisors returns the number of (positive) divisors of x. Uses PrimeFactorisation(x). Does not handle math.MinInt64.

func NumberOfDivisorsBruteForce added in v1.1.1

func NumberOfDivisorsBruteForce(x int) int

NumberOfDivisorsBruteForce returns the number of (positive) divisors of x. Uses a brute force method.

func Pow

func Pow(x, y int) int

Pow returns the x^|y|.

func PrimeFactorisation

func PrimeFactorisation(x int) <-chan PrimeFactor

PrimeFactorisation sends the prime factorisation of |x| on a channel, in ascending order. If x is 0 or 1, PrimeFactorisation(x) returns a PrimeFactor with value x, index 1. Does not handle math.MinInt64.

func SumOfDivisors

func SumOfDivisors(x int) int

SumOfDivisors returns the sum of all (positive) divisors of x. Uses PrimeFactorisation(x). Does not handle math.MinInt64.

func SumOfDivisorsBruteForce added in v1.1.1

func SumOfDivisorsBruteForce(x int) int

SumOfDivisorsBruteForce returns the sum of all (positive) divisors of x. Uses a brute force method.

Types

type PrimeFactor

type PrimeFactor struct {
	Value, Index int
}

PrimeFactor is designed to hold a prime number as 'value' and the index of the prime number when it appears in a complete prime factorisation product.

type Tree

type Tree struct {
	Left  *Tree
	Value int
	Right *Tree
}

A Tree has a value and two sub trees.

func CreateBinaryTree

func CreateBinaryTree(values ...int) *Tree

CreateBinaryTree returns a (mostly) symmetric binary tree, filling with values from top to bottom, left to right. CreateBinaryTree() returns <nil>

func CreatePyramidTree

func CreatePyramidTree(values ...int) *Tree

CreatePyramidTree returns a (mostly) symmetric pyramid tree, filling with values from top to bottom, left to right. CreatePyramidTree() returns <nil>

Jump to

Keyboard shortcuts

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