How can I maximally partition a set?

后端 未结 6 1248
既然无缘
既然无缘 2021-01-03 14:17

I\'m trying to solve one of the Project Euler problems. As a consequence, I need an algorithm that will help me find all possible partitions of a set, in any order.

6条回答
  •  天涯浪人
    2021-01-03 15:02

    Here is the code you need for this part of your problem:

    def memoize(f):
        memo={}
        def helper(x):
            if x not in memo:
                memo[x]=f(x)
            return memo[x]
        return helper
    
    @memoize
    def A000041(n):
        if n == 0: return 1
        S = 0
        J = n-1
        k = 2
        while 0 <= J:
            T = A000041(J)
            S = S+T if k//2%2!=0 else S-T
            J -= k if k%2!=0 else k//2
            k += 1
        return S
    
    print A000041(100) #the 100's number in this series, as an example
    

提交回复
热议问题