projecteuler

package module
v0.0.0-...-5f64281 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 14 Imported by: 0

README

projecteuler

projecteuler problems GoLang solutions

Documentation

Index

Constants

View Source
const (
	// RecursiveKind is self-explanatory, you stupid Lint
	RecursiveKind = iota
	// ConcurrentKind is self-explanatory, you stupid Lint
	ConcurrentKind
)

Variables

This section is empty.

Functions

func Binomial

func Binomial(n, k int, primes []int) (result map[int]int, err error)

Binomial return binomial coefficient of n choose k

func Combinations

func Combinations(n, k byte, f func(...interface{}) bool, args ...interface{}) (
	combinations [][]byte, retValue bool)

Combinations calculates and returns combinations of k out of n elements, or until f returns true. n has to be greater or equal to k. Each slice in the resulting combination matrix has 1s per elements used in a particular combination.

func CompareBigInts

func CompareBigInts(l, r BigInt) int

CompareBigInts returns -1, 0, 1 for l < r, l == r, l > r respectively

func CompareFactors

func CompareFactors(f1, f2 map[int]int) bool

CompareFactors comapares factors of two numbers returning true if equal

func DecryptString

func DecryptString(encryptedText string, gcm cipher.AEAD) (text string, err error)

DecryptString decrypts

func EncryptString

func EncryptString(txt string, gcm cipher.AEAD) (encryptedText string, err error)

EncryptString encrypts

func Factorial

func Factorial(n int, primes []int) (factors map[int]int, err error)

Factorial returns factorial of n as map of its prime factors

func Factorize

func Factorize(num int, primes []int) (factors map[int]int, err error)

Factorize returns prime factorization of num. Returns error if primes doesn't contain all prime factors of num

func FileToStrings

func FileToStrings(fileName string) (textLines []string, err error)

FileToStrings reads the entire file and returns its lines in slice of strings

func FindDivisors

func FindDivisors(factors map[int]int) (divisors []int)

FindDivisors takes factors map and returns slice of divisors

func FuncForTesting

func FuncForTesting(expectedEncryptedString string, f funcToTest, args ...interface{}) (err error)

FuncForTesting is generic integration test function

func GCD

func GCD(m, n int64) int64

GCD returns greatest common divisor of m and n. GCD(m, n) == GCD(n, m % n)

func GenerateGcm

func GenerateGcm() (gcm cipher.AEAD, err error)

GenerateGcm creates cipher key

func IsHexagonal

func IsHexagonal(x int, roots map[int]int) bool

IsHexagonal returns if x is hexagonal

func IsPentagonal

func IsPentagonal(x int, roots map[int]int) bool

IsPentagonal returns if x is pentagonal

func IsPrime

func IsPrime(x int64) bool

IsPrime returns true iff x is prime

func IsTriangular

func IsTriangular(x int, roots map[int]int) bool

IsTriangular returns if x is triangular

func ModuloMultiply

func ModuloMultiply(a, b int64, mod int64) (result int64)

ModuloMultiply calculates multiplication module mod

func ModuloSelfPower

func ModuloSelfPower(x int, mod int64) (selfPower int64)

ModuloSelfPower calculates self power of x module mod

func ModuloSum

func ModuloSum(a, b int64, mod int64) (sum int64)

ModuloSum calculates sum module mod

func MulBigIntVectors

func MulBigIntVectors(a, b *BigIntVector) (result *big.Int)

MulBigIntVectors multiplies arguments and puts the result in the new big.Int

func MultiplyFactors

func MultiplyFactors(primeFactors map[int]int) int64

MultiplyFactors multiplies factors

func NumDividedByTotient

func NumDividedByTotient(n int, primes []int) float64

func NumberFromDigits

func NumberFromDigits(digits []byte) (value int)

NumberFromDigits calculates number from its reversed digits

func Permutations

func Permutations(limit byte, f func(...interface{}) bool, args ...interface{}) (permutations [][]byte)

Permutations calculates and returns permutations of limit elements, or until f returns true

func PrimeSet

func PrimeSet(limit int) (primes []int, primeSet map[int]struct{})

PrimeSet calculates and returns slice of primes less than limit and puts them in primeSet

func Primes

func Primes(limit int, f func(...interface{}) bool, args ...interface{}) (primes []int)

Primes calculates and returns slice of primes smaller than limit, or until f returns true

func PrintMatrix

func PrintMatrix(m [][]int)

PrintMatrix prints matrix

func ReverseSquares

func ReverseSquares(limit int) (roots map[int]int)

ReverseSquares calculates and returns a map of squares to roots for roots less than limit

func Timed

func Timed(f func(...interface{}) (string, error), args ...interface{})

Timed executes the function and displays its execution time

func Totient

func Totient(n int, primes []int) int

Types

type BigInt

type BigInt struct {
	// contains filtered or unexported fields
}

BigInt is a struct holding slice of digits in reversed order

func AddBigInts

func AddBigInts(one BigInt, two BigInt) (result BigInt)

AddBigInts adds

func MakeBigInt

func MakeBigInt(input string) (result BigInt)

MakeBigInt constructs BigInt out of string

func MakeBigIntFromInt

func MakeBigIntFromInt(input int) (result BigInt)

MakeBigIntFromInt constructs BigInt out of int

func MakeZeroBigInt

func MakeZeroBigInt() BigInt

MakeZeroBigInt constructs zero BigInt

func MulBigInts

func MulBigInts(one BigInt, two BigInt) (result BigInt)

MulBigInts multiplies BigInts

func (*BigInt) AddTo

func (bi *BigInt) AddTo(rhs BigInt)

AddTo adds to bi

func (BigInt) Clone

func (bi BigInt) Clone() (result *BigInt)

Clone clones BigInt

func (*BigInt) Concatenate

func (bi *BigInt) Concatenate(rhs BigInt)

Concatenate appends rhs digits to bi

func (BigInt) DigitCount

func (bi BigInt) DigitCount() int

DigitCount returns digit count

func (BigInt) DigitSum

func (bi BigInt) DigitSum() int

DigitSum returns sum of the digits

func (BigInt) Digits

func (bi BigInt) Digits() []byte

Digits returns copy of reversed digits

func (BigInt) Int

func (bi BigInt) Int() int64

Int returns int64 value of bi. If bi respresents too big number, int64 will overflow.

func (BigInt) IsPalindrome

func (bi BigInt) IsPalindrome() bool

IsPalindrome returns true iff bi is a palindrome

func (*BigInt) MulPowTen

func (bi *BigInt) MulPowTen(pow int)

MulPowTen multiplies BigInt with power of ten

func (*BigInt) MultiplyByDigit

func (bi *BigInt) MultiplyByDigit(d byte)

func (*BigInt) PowBigInt

func (bi *BigInt) PowBigInt(pow int)

PowBigInt returns power of BigInt

func (*BigInt) ReverseDigits

func (bi *BigInt) ReverseDigits()

ReverseDigits reverses digits of bi

func (BigInt) SquareRoot

func (bi BigInt) SquareRoot(decimalCount int) (root BigInt, decimalIndex int)

func (BigInt) String

func (bi BigInt) String() string

String returns string representation

func (*BigInt) Subtract

func (bi *BigInt) Subtract(rhs BigInt)

Subtract subtracts from bi

type BigIntFraction

type BigIntFraction struct {
	// contains filtered or unexported fields
}

BigIntFraction struct

func CalcElements

func CalcElements(elements []*RootIntElement) BigIntFraction

CalcElements calculates and returns BigIntFraction value of the continued fraction represented by elements

func MakeFraction

func MakeFraction(numerator, denominator *big.Int) (fr BigIntFraction)

MakeFraction creates and returns fr

func (*BigIntFraction) AddInt

func (fr *BigIntFraction) AddInt(x int64)

AddInt adds x to receiver

func (BigIntFraction) Denominator

func (fr BigIntFraction) Denominator() *big.Int

Denominator returns receiver's denominator

func (*BigIntFraction) Invert

func (fr *BigIntFraction) Invert()

Invert flips receiver's numerator and denominator

func (BigIntFraction) Numerator

func (fr BigIntFraction) Numerator() *big.Int

Numerator returns receiver's numerator

type BigIntMatrix

type BigIntMatrix struct {
	// contains filtered or unexported fields
}

BigIntMatrix struct

func MulBigIntMatrix

func MulBigIntMatrix(a, b *BigIntMatrix) (result *BigIntMatrix)

MulBigIntMatrix multiplies arguments and puts the result in the new BigIntMatrix

func NewBigIntMatrix

func NewBigIntMatrix(x, y int, m []int64) (result *BigIntMatrix, err error)

NewBigIntMatrix constructs new BigIntMatrix

func NewUnitBigIntMatrix

func NewUnitBigIntMatrix(x int) (result *BigIntMatrix, err error)

NewUnitBigIntMatrix constructs new unit BigIntMatrix

func (*BigIntMatrix) At

func (a *BigIntMatrix) At(y, x int) (result *big.Int, err error)

At returns element at (y,x)

func (*BigIntMatrix) Clone

func (a *BigIntMatrix) Clone() (result *BigIntMatrix)

Clone copies receiver to new BigIntMatrix

func (*BigIntMatrix) Dim

func (a *BigIntMatrix) Dim() (x, y int)

Dim returns length of the BigIntMatrix

func (*BigIntMatrix) Print

func (a *BigIntMatrix) Print()

Print prints the BigIntMatrix

type BigIntVector

type BigIntVector struct {
	// contains filtered or unexported fields
}

BigIntVector struct

func NewBigIntVector

func NewBigIntVector(vec []int64) (result *BigIntVector, err error)

NewBigIntVector constructs new BigIntVector from slice of int64

func NewVectorFromBigIntSlice

func NewVectorFromBigIntSlice(vec []*big.Int) (result *BigIntVector, err error)

NewVectorFromBigIntSlice constructs new BigIntVector from slice of *big.Int

func (*BigIntVector) Clone

func (a *BigIntVector) Clone() (result *BigIntVector)

Clone copies receiver to new BigIntVector

func (*BigIntVector) Dim

func (a *BigIntVector) Dim() int

Dim returns length of the BigIntVector

type ChakravalaTriplet

type ChakravalaTriplet struct {
	X *big.Int
	Y *big.Int
	K int64
}

ChakravalaTriplet holds X, Y, K which are solution to X^2 - N*Y^2 = K

func Chakravala

func Chakravala(n int) ChakravalaTriplet

Chakravala returns minimal solution to X^2 - N*Y^2 = 1. It uses Chakravala method, commonly attributed to Bhāskara II, a 12th century mathematician. His work was building on earlier works by Jayadeva (10th century) and Brahmagupta (7th century). Quality and mathematical depth of works by Indian mathematicians regarding numbers and algebra in general were only reached by European mathematicians much, much later

func Samasa

func Samasa(n int, m *big.Int, prev ChakravalaTriplet) ChakravalaTriplet

func (ChakravalaTriplet) String

func (c ChakravalaTriplet) String() string

type ConcurrentStringSet

type ConcurrentStringSet struct {
	// contains filtered or unexported fields
}

func NewConcurrentStringSet

func NewConcurrentStringSet() ConcurrentStringSet

func (*ConcurrentStringSet) Read

func (css *ConcurrentStringSet) Read() []string

func (*ConcurrentStringSet) Write

func (css *ConcurrentStringSet) Write(s string)

type ContinuedFraction

type ContinuedFraction struct {
	RootFloor int

	Head      RootIntElement
	Fractions []RootIntElement
	// contains filtered or unexported fields
}

ContinuedFraction holds

func MakeContinuedFraction

func MakeContinuedFraction(x int, primes []int) (c ContinuedFraction)

MakeContinuedFraction creates and returns c, a continued fraction representation of sqrt(x). primes is a slice containing primes. It is necessary for gcd calculations important for reducing fractions

func (ContinuedFraction) Convergent

func (c ContinuedFraction) Convergent(elementCount int) BigIntFraction

Convergent calculates BigIntFraction value of elementCount convergent

func (ContinuedFraction) Period

func (c ContinuedFraction) Period() int

Period returns a period of receiver

func (ContinuedFraction) String

func (c ContinuedFraction) String() string

String returns concise receiver's string representation

type DigitalNumber

type DigitalNumber struct {
	// contains filtered or unexported fields
}

DigitalNumber is immutable structure containing number and it's digits

func NewDigitalNumber

func NewDigitalNumber(x int) (newDn DigitalNumber)

NewDigitalNumber constructs new DigitalNumber

func (DigitalNumber) DifferentDigitCompositions

func (dn DigitalNumber) DifferentDigitCompositions(usedDigits map[byte]struct{}) (differentDigits bool)

DifferentDigitCompositions returns true iff DigitalNumber's digits are not in the set, and adds them to it if so. Caller is responsible for making the set

func (DigitalNumber) DifferentDigits

func (dn DigitalNumber) DifferentDigits() bool

DifferentDigits returns true iff digits are different among themselves

func (DigitalNumber) DigitCount

func (dn DigitalNumber) DigitCount() int

DigitCount returns number of digits

func (DigitalNumber) DigitOccurencies

func (dn DigitalNumber) DigitOccurencies() (occurencies map[byte]int)

DigitOccurencies returns the map of digit occurencies

func (DigitalNumber) Digits

func (dn DigitalNumber) Digits() []byte

Digits returns digits (reference)

func (DigitalNumber) NonZeroDigits

func (dn DigitalNumber) NonZeroDigits() bool

NonZeroDigits returns true iff digits don't contain zero

func (DigitalNumber) ReplaceDigits

func (dn DigitalNumber) ReplaceDigits(replacements map[byte]byte) (value int)

ReplaceDigits replaces digits in a number with ones from the map (index->new digit)

func (DigitalNumber) SameDigitSet

func (dn DigitalNumber) SameDigitSet(d2 DigitalNumber) bool

SameDigitSet returns true iff d2 has the same multiset of digits as dn

func (DigitalNumber) X

func (dn DigitalNumber) X() int

X returns number

type FibonacciBox

type FibonacciBox struct {
	A, B, C, D int
}

FibonacciBox is a representation of matrix | A B |

| C D |,

where A, B are coprime, and B, A, C, D is a generalized Fibonacci sequence, i.e. C = B + A, D = C +A. That implies the box can be generated from 2 numbers. Every box singularily represents a Pythagorean triplet: At = 2AC, Bt = BD, Ct = BC + AD

func (FibonacciBox) Triplet

func (box FibonacciBox) Triplet() PythagoreanTriplet

type FibonacciBoxTernaryTree

type FibonacciBoxTernaryTree struct {
	Root   *FibonacciBox
	Child1 *FibonacciBoxTernaryTree
	Child2 *FibonacciBoxTernaryTree
	Child3 *FibonacciBoxTernaryTree
}

func NewFibonacciBoxTernaryTree

func NewFibonacciBoxTernaryTree(a, b, c, d int) FibonacciBoxTernaryTree

func (*FibonacciBoxTernaryTree) Generate

func (tree *FibonacciBoxTernaryTree) Generate(limit int)

func (*FibonacciBoxTernaryTree) TripletSlice

func (tree *FibonacciBoxTernaryTree) TripletSlice() []PythagoreanTriplet

type Fraction

type Fraction struct {
	Num   int64
	Denom int64
}

func NewFraction

func NewFraction(num, denom int64) Fraction

func (Fraction) Less

func (f Fraction) Less(rhs Fraction) bool

func (*Fraction) Reduce

func (f *Fraction) Reduce()

f.num == n1*k, f.denom == d1*k, where k == GCD(num,denom). After reducing, f.num == n1 and f.denom == d1

func (Fraction) String

func (f Fraction) String() string

type Oker

type Oker interface {
	Ok() bool
}

Oker has Ok() bool

type OneResultSolver

type OneResultSolver interface {
	OneResultSolve(jobs <-chan interface{}) (interface{}, error)
}

OneResultSolver has OneResultSolve(jobs <-chan interface{}) (interface{}, error)

func OneResultSolverFactory

func OneResultSolverFactory(kind int, f workerFunctionType) OneResultSolver

OneResultSolverFactory produces OneResultSolver appropriate per kind

type PythagoreanTriplet

type PythagoreanTriplet struct {
	A, B, C int
}

PythagoreanTriplet represents triplet for which A^2 + B^2 = C^2

func (PythagoreanTriplet) Length

func (triplet PythagoreanTriplet) Length() int

func (PythagoreanTriplet) Multiply

func (triplet PythagoreanTriplet) Multiply(k int) PythagoreanTriplet

func (PythagoreanTriplet) String

func (triplet PythagoreanTriplet) String() string

type RootIntElement

type RootIntElement struct {
	Head      int64
	Fractions rootIntFraction
}

RootIntElement holds Head (integer part) and Fractions, rootIntFraction

Jump to

Keyboard shortcuts

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