[Python] How do I create a mathematical formula for python that will get the numbers in order...

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm coding in python and I need a closed form mathematical formula that takes a variable which is the result of a group of numbers added together, and allows me to use sympy to get the numbers in order from when they were added.

    For example if I have the numbers 1 2 3 4 and 5 all added together in that order (1+2+3+4+5), how do I take the result in a variable which will be equal to 15 and get the the numbers in the order they were added (1,2,3,4,5) in each step of my sympy summation?

    My code is:

    from sympy import symbols, summation, Eq

    # Define the variable and the summation range
    i = symbols('i')

    # Define a simple expression to sum, like i from 1 to 5
    expr = 20 + 30 + 50 + 60 + 70

    formula_to_get_order = expr + i #just a placeholder since I don't know the correct formula

    sum_result = summation(expr, (i, 1, 5))

    print("Summation result:", sum_result)


    This is the code. It just takes the numbers in expr and adds them together. I want to get the order of the values by using i in my sympy summation. I don't want any loops I want specifically a closed form mathematical formula that will do this. I need a closed form mathematical formula to do this.

    For each step of i in the summation, the values should be 1:20 2:30 3:50 4:60 and so on. I am going to be doing this for a much larger range so it should be a mathematical formula.

    Continue reading...

Compartilhe esta Página