Gautam Sagar
Gautam Sagar

Reputation: 63

Positive Distinct Summands in Python

This Code is giving wrong output for n=2 and it is very slow.

How Can i make this code for finding as many positive distinct summands more efficient?

TASK- Represent a given positive integer n as a sum of as many pairwise distinct positive integers as possible.

OUTPUT: First Contains an integer k.Second Line Contains k positive distinct positive summands that sum up to n.

Sample Input:

8

Output:

3

1 2 5

n=int(input())
p=[]
i=1
while(i<=n):
      if (n-i) not in p:
          p.append(i)
      n-=i
      i+=1
print(len(p))
print(*p)

Upvotes: 3

Views: 2471

Answers (3)

Moh
Moh

Reputation: 188

One way to find the sequence is to have two starting points: N and m, where N is the number we are trying to reach and m starts at 1.

Next, we increase m by 1, add it to the sequence and reduce N. The solution is reached by reducing N by m and stopping when N <= m *2.

For example, if the number that we are trying to reach is 3

N = 3

Then,

s={} #  array to store pairwise distinct positive integers
k = 3 # k = N at the beginning 
m = 1  #  always starts at 1 
m*2 = 2

since

 k is not <= m*2

we add m to the array, subtract m from k, add 1 to m and repeat the same process again.

s = {1}
k = k - m = 3 - 1 = 2
m = 1 + m = 1 + 1 = 2

Now,

m*2 = 4

since

 k <= m*2  

We add k to our array and we are done.

 s = {1, 2}

Here is the code in python that does the same thing.

def optimal_summands(n):

 summands = [] # array to store pairwise distinct positive integers
 k = n # marked the upper part
 m = 1 # marked the lower part

 if n == 2 or n == 1:
     summands.append(n)
 else:
     for i in range(1,k):
         if k <= 2*m:
             summands.append(k)
             break
         else:
             summands.append(m)
             k = k - m
             m = m + 1

 return summands

Upvotes: 2

user3386109
user3386109

Reputation: 34839

You can solve the problem analytically. If the number you're trying to reach is N, then the answer will always be

1+2+3+ ... +n + r = N

where n is the largest possible number such that n < r. For example, take N=8, and consider the possible values of n

n   sum(1..n)  r
0       0      8
1       1      7
2     1+2=3    5
3    1+2+3=6   2    // too high, n is not less than r

Thus, when N is 8, n is 2 and r is 5, giving an answer of 1+2+5.

So the question becomes, given the value of N, how can we compute n. The first step is to note that the sum of 1 thru n is given by the equation

1+2+3+ ... +n = n(n+1)/2

Plug that into the first equation

n(n+1)/2 + r = N

Using the fact that n < r, we can rewrite this as

n(n+1)/2 + n < N

enter image description here

And that's the answer that you need to implement. For example, if N is 2, then the formula says n < 1 which means that n is 0 and r is 2. If N is 8, then n < 2.77, which means that n is 2 and r is 5.

Upvotes: 9

kcborys
kcborys

Reputation: 316

I'm not sure if your code has other flaws but I can see that there are test cases similar to n=2 that will make this fail (I believe n=9 will, for example). Try if ((n-i) not in p and n-i != i). You fail n=2 because it sees that i=1 is not in p, yet you are just about to put it in.

Upvotes: 0

Related Questions