خرید بک لینک

Vote count: 0

The aim of this assignment is to find the maximum number of numbers that sum to a number n, such that these numbers are unique. For example, if n=8, we start with l=1 then subtract 1 from n to get 7 then try l=2 giving k=5, but then we stop because some of the distinct sums of this number are members of the previous list. So I'm trying to implement an iterative approach. I've already tried a recursive approach but that reaches maximum recursive depth because n <= 10^9. I am actually trying a recursive approach here to checking if the distinct sums of k are in the list summands but this doesn't work. For an input of n = 85, the output of this is [1, 84] whereas the correct output is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 19]. What am I missing? Thanks in advance.

def optimalSummandsIter(n):
        '''
        The goal of this problem is to represent
        a given positive integer n as a sum of as many pairwise
        distinct positive integers as possible.
        In the first line, output the maximum number k such that n can be represented as a sum
        of k pairwise distinct positive integers. 
        In the second line, output k pairwise distinct positive integers
        that sum up to n (if there are many such representations, output any of them).
        Initially we have k = n and l = 1. 
        To solve a (k, l)-subproblem, we do the following. 
        If k ≤ 2l, we output just one summand k. 
        Otherwise we output l and then solve the subproblem (k − l, l + 1)
        '''
        summands = []
        k = n
        l = 1
        m = sum(summands)   
        if k <= 2*l:
            summands.append(k)
            retu summands
        while k > 0:
            if any(i in optimalSummandsIter(k) for i in summands):
                summands.append(k)
                retu summands
            else:
                summands.append(l)
                k -= l
                l += 1

        retu summands

asked 27 secs ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 25 ارديبهشت 1395 ساعت: 11:35

صفحه بندی