Skip to content

Commit

Permalink
add some OEIS links
Browse files Browse the repository at this point in the history
  • Loading branch information
EndlessCheng committed Jun 18, 2020
1 parent 257b224 commit 80134db
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
1 change: 0 additions & 1 deletion copypasta/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
func commonCollection() {
const alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
pow10 := [...]int{1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9} // math.Pow10
factorial := [...]int{1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 /*10!*/, 39916800, 479001600}
// TIPS: dir4[i] 和 dir4[i^1] 互为相反方向
type pair struct{ x, y int }
dir4 := [...]pair{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} // 上下左右
Expand Down
41 changes: 30 additions & 11 deletions copypasta/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,11 +650,12 @@ func numberTheoryCollection() {
}

// 欧拉函数(互质的数的个数)Euler totient function https://oeis.org/A000010
// 预处理所有 [2,mx] 数的欧拉函数
// NOTE: phi 的迭代(指 phi[phi...[n]])是 log 级别收敛的:奇数减一,偶数减半
phiAll := func() {
// https://en.wikipedia.org/wiki/Euler%27s_totient_function
// 预处理 [1,mx] 欧拉函数
// NOTE: phi[phi...[n]] 收敛到 1 的迭代次数是 log 级别的:奇数减一,偶数减半 https://oeis.org/A003434
initPhi := func() {
const mx int = 1e6
phi := [mx + 1]int{}
phi := [mx + 1]int{1: 1}
for i := 2; i <= mx; i++ {
phi[i] = i
}
Expand All @@ -680,7 +681,7 @@ func numberTheoryCollection() {

// Unitary totient (or unitary phi) function uphi(n) http://oeis.org/A047994

/* 同余 */
/* 同余 逆元 */

// 二元一次不定方程
// exgcd solve equation ax+by=gcd(a,b)
Expand Down Expand Up @@ -838,11 +839,12 @@ func numberTheoryCollection() {
// todo
// 模板题 https://www.luogu.com.cn/problem/P5491 https://www.luogu.com.cn/problem/P5668

/* 组合数;二项式系数
*/
/* 阶乘 组合数/二项式系数 */

// 阶乘
factorial := func(n int) int64 {
// https://oeis.org/A000142
factorial := [...]int{1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 /*10!*/, 39916800, 479001600}

calcFactorial := func(n int) int64 {
res := int64(1) % mod
for i := 2; i <= n; i++ {
res = res * int64(i) % mod
Expand Down Expand Up @@ -1060,14 +1062,25 @@ func numberTheoryCollection() {
// 浅谈一类积性函数的前缀和 + 套题 https://blog.csdn.net/skywalkert/article/details/50500009
// 模板题 https://www.luogu.com.cn/problem/P4213

// 埃及分数 - 不同的单位分数的和 (IDA*)
// https://www.luogu.com.cn/problem/UVA12558
// 贪婪算法:将一项分数分解成若干项单分子分数后的项数最少,称为第一种好算法;最大的分母数值最小,称为第二种好算法
// https://en.wikipedia.org/wiki/Egyptian_fraction
// https://oeis.org/A006585 number of solutions
// https://oeis.org/A247765 Table of denominators in the Egyptian fraction representation of n/(n+1) by the greedy algorithm
// https://oeis.org/A100678 Number of Egyptian fractions in the representation of n/(n+1) via the greedy algorithm
// https://oeis.org/A100695 Largest denominator used in the Egyptian fraction representation of n/(n+1) by the greedy algorithm
//
// 埃尔德什-施特劳斯猜想(Erdős–Straus conjecture)https://en.wikipedia.org/wiki/Erd%C5%91s%E2%80%93Straus_conjecture

_ = []interface{}{
primes,
sqCheck, cubeCheck, sqrt, cbrt, bottomDiff,
gcd, gcdPrefix, gcdSuffix, lcm, frac, cntRangeGCD,
isPrime, sieve, primeFactorization, primeDivisors, primeExponentsCountAll,
divisors, divisorPairs, doDivisors, doDivisors2, oddDivisorsNum, maxSqrtDivisor, divisorsAll, primeFactorsAll, lpfAll, distinctPrimesCountAll, calcPhi, phiAll,
divisors, divisorPairs, doDivisors, doDivisors2, oddDivisorsNum, maxSqrtDivisor, divisorsAll, primeFactorsAll, lpfAll, distinctPrimesCountAll, calcPhi, initPhi,
exgcd, invM, invP, divM, divP, initAllInv, crt, excrt, babyStepGiantStep,
factorial, initFactorial, _factorial, combHalf, comb,
factorial, calcFactorial, initFactorial, _factorial, combHalf, comb,
muInit,
}
}
Expand Down Expand Up @@ -1111,6 +1124,12 @@ Stern-Brocot 树与 Farey 序列 https://oi-wiki.org/misc/stern-brocot/ https://
https://en.wikipedia.org/wiki/Generating_function
整数分拆 https://oeis.org/A000041 https://en.wikipedia.org/wiki/Partition_(number_theory)
质数分拆
https://oeis.org/A061358 Number of ways of writing n=p+q with p, q primes and p>=q
https://oeis.org/A067187 Numbers that can be expressed as the sum of two primes in exactly one way
https://oeis.org/A068307 number of partitions of n into a sum of three primes
https://oeis.org/A071335 Number of partitions of n into a sum of at most three primes
记 A = [1,2,...,n],A 的全排列中与 A 的最大差值为 n^2/2 https://oeis.org/A007590
Maximum sum of displacements of elements in a permutation of (1..n)
For example, with n = 9, permutation (5,6,7,8,9,1,2,3,4) has displacements (4,4,4,4,4,5,5,5,5) with maximal sum = 40
Expand Down
34 changes: 18 additions & 16 deletions copypasta/math_continued_fraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ func continuedFractionCollections() {
}

// sqrt(d) = [exp[0]; exp[1],..., 2*exp[0], exp[1], ..., 2*exp[0], exp[1], ...]
// https://en.wikipedia.org/wiki/Pell%27s_equation
// https://en.wikipedia.org/wiki/Pell%27s_equation 解 https://oeis.org/A002350 https://oeis.org/A002349
// https://www.weiwen.io/post/about-the-pell-equations-2/
// 连分数表示 https://oeis.org/A240071
// 循环节长度 https://oeis.org/A003285
calcSqrtContinuedFraction := func(d int64) (exp []int64) {
sqrtD := math.Sqrt(float64(d))
base := int64(sqrtD)
Expand Down Expand Up @@ -77,16 +79,16 @@ func continuedFractionCollections() {
// return
//}

calcGCD := func(a, b int64) int64 {
gcd := func(a, b int64) int64 {
for b > 0 {
a, b = b, a%b
}
return a
}
calcGCDN := func(nums ...int64) (gcd int64) {
gcd = nums[0]
for _, v := range nums[1:] {
gcd = calcGCD(gcd, v)
gcds := func(a ...int64) (g int64) {
g = a[0]
for _, v := range a[1:] {
g = gcd(g, v)
}
return
}
Expand All @@ -111,11 +113,11 @@ func continuedFractionCollections() {
if newC == 0 {
return
}
gcd := calcGCDN(newA, newB, newC)
//Println(i, base, newA, newB, newC, gcd)
a = append(a, newA/gcd)
b = append(b, newB/gcd)
c = append(c, newC/gcd)
g := gcds(newA, newB, newC)
//Println(i, base, newA, newB, newC, g)
a = append(a, newA/g)
b = append(b, newB/g)
c = append(c, newC/g)
}
return
}
Expand Down Expand Up @@ -147,14 +149,14 @@ func continuedFractionCollections() {
if newC.IsInt64() && newC.Int64() == 0 {
return
}
gcd := big.NewInt(1)
g := big.NewInt(1)
if !base.IsInt64() || base.Int64() > 0 {
gcd.GCD(nil, nil, newA, newB).GCD(nil, nil, gcd, newC)
g.GCD(nil, nil, newA, newB).GCD(nil, nil, g, newC)
}
//Println(i, base, newA, newB, newC, gcd)
a = append(a, newA.Quo(newA, gcd))
b = append(b, newB.Quo(newB, gcd))
c = append(c, newC.Quo(newC, gcd))
a = append(a, newA.Quo(newA, g))
b = append(b, newB.Quo(newB, g))
c = append(c, newC.Quo(newC, g))
}
return
}
Expand Down

0 comments on commit 80134db

Please sign in to comment.