runtime - My 4 python loops take too long -


this code taking long finish. did know make work , left running hour. trying solve problem:

let a, b, c, , n positive integers. if a+b+c = 19*97 , a+n = b-n = c/n compute value of a.

here code:

from itertools import * import gc gc.disable() a_list = [] ah = 0 b_num = 0 c_num = 0 number_of_solutions = 0 a, b, c, n in product(range(1, 19*97), repeat=4):         if a+b+c == 19*97 , a+n == b-n , b-n == c/n , a+n == c/n:             a_num =             b_num = b             c_num = c             a_list.append(a)             number_of_solutions = number_of_solutions + 1         else:             pass  print a_num+b_num+c_num print a_list print number_of_solutions 

please help! thanks!

brute-forcing possible combinations inefficient. instead, algebra. eliminate variables.

(a+n) + (b-n) == (c/n) + (c/n) a+b == 2*c/n a+b+c == 2*c/n + c == 19*97 c*(2+n) == 19*97*n 

since a+n == c/n , a, b, c, , n positive integers, c multiple of n, so

(2+n) * (c/n) == 19*97 

(2+n) must 19, 97, or 19*97. can rest.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -