github.com/zhiqiangxu/util@v0.0.0-20230112053021-0a7aee056cd5/math/partition.go (about)

     1  package math
     2  
     3  import "math/big"
     4  
     5  func Partition(n int) *big.Int {
     6  	if n <= 0 {
     7  		panic("invalid input")
     8  	}
     9  
    10  	// Euler's method
    11  	var current, next *Polynomial
    12  	for i := 1; i <= n; i++ {
    13  		coefficients := make([]*big.Int, n+1)
    14  		for j := 0; j <= n; j++ {
    15  			if j%i == 0 {
    16  				coefficients[j] = big.NewInt(1)
    17  			} else {
    18  				coefficients[j] = big.NewInt(0)
    19  			}
    20  		}
    21  
    22  		next = NewPolynomialWithMaxOrder(n, coefficients)
    23  		if current == nil {
    24  			current = next
    25  		} else {
    26  			current = current.Mul(next)
    27  		}
    28  	}
    29  
    30  	return current.coefficients[n]
    31  }