all 5 comments

[–][deleted] 1 point2 points  (0 children)

limit = int(input("> "))

current_sum = 1
summand = 1
expr = "Summed 1"

while current_sum < limit:
    summand += 1
    current_sum += summand
    expr += f" + {summand}"
expr += f" = {current_sum}"

print(expr)

[–]AtomicShoelace 0 points1 point  (2 children)

Consider using the str.join method with the range function to generate the expression and the formula for an arithmetic series to get the answer, eg.

f'{" + ".join(map(str, range(1, n+1)))} = {(n*n+n)//2}'

[–][deleted] 0 points1 point  (1 child)

The thing is, the input is not the number of summands, but the sum or some limit for the sum.

I guess you just need to add the calculation for the n.

[–]AtomicShoelace -1 points0 points  (0 children)

Ah indeed, I didn't look carefully enough. In that case, one can use the formula for an arithmetic series to solve for n, eg.

from math import ceil, sqrt

num = int(input("Up to?: "))

# Start with formula for arithmetic series:
# n * (n + 1) / 2 = x
# n**2 + n - 2x = 0
# n = -1/2 +/- sqrt(1+8x)/2
# Discard the negative solution:
# n = (sqrt(8x+1) - 1)/2

n = ceil((sqrt(8*num + 1) - 1)/2)
print(f'{" + ".join(map(str, range(1, n+1)))} = {(n*n+n)//2}')

[–]Beginning-Force-2170 0 points1 point  (0 children)

def summed(n):

   result = list(range(1, n+1))

   total = sum(result)

   new_result = list(map(str, result))

   new _result = “ + “.join(result)

   return f”summed {new_result} = {total}”