Making maths .py program faster - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Making maths .py program faster (/thread-8355.html) |
Making maths .py program faster - Shutcois - Feb-16-2018 Hello! I just started learning python this week and i'm struggling with this program. It's a program that gets one input, being a float with 2 decimals called t, and is supposed to give back a, b and c, also floats with 2 decimals. The conditions are: a * b * c == t a + b + c == t 0 <= a <= b <= c I already have the code and it works! But it's not fast enough. Anyone has ideas to make it faster? https://hastebin.josephbanks.me/xamohiwenu.py RE: Making maths .py program faster - nilamo - Feb-16-2018 (Feb-16-2018, 05:22 PM)Shutcois Wrote: https://hastebin.josephbanks.me/xamohiwenu.py Quote:som = float(input()) totaal = int(som*100) totaal2 = int(totaal*10000) def sum_to_n(n, size, limit=None): """Produce all lists of size positive integers in decreasing order that add up to n.""" if size == 1: yield [n] return if limit is None: limit = n start = (n + size - 1) // size stop = min(limit, n - size + 1) + 1 for i in range(start, stop): for tail in sum_to_n(n - i, size - 1, i): yield [i] + tail for partition in sum_to_n(totaal, 3): if partition[0] * partition[1] * partition[2] == totaal2 and partition[0] + partition[1] + partition[2] == totaal: c = partition[0] b = partition[1] a = partition[2] break print(f"${format(a/100, '.2f')} + ${format(b/100, '.2f')} + ${format(c/100, '.2f')} = ${format(a/100, '.2f')} x ${format(b/100, '.2f')} x ${format(c/100, '.2f')} = ${format(som, '.2f')}") Added your code for you :) |