Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- If a, b, c, d are positive integers with sum = 63 what is the maximum value
- of the expression ab + bc + cd
- '''
- from timeit import default_timer as timer
- def maximize(sum):
- maxExpression = 0
- maxA = 0
- maxB = 0
- maxC = 0
- maxD = 0
- start = timer()
- for a in range(1, sum):
- for b in range(1, sum):
- for c in range(1, sum):
- for d in range(1, sum):
- if a + b + c + d == sum:
- expression = a * b + b * c + c * d
- flag = True
- if expression > maxExpression:
- maxExpression = expression
- maxA = a
- maxB = b
- maxC = c
- maxD = d
- end = timer()
- elapsed = end - start
- print("We know that a,b,c,d > 0 and: a + b + c + d = " + str(sum))
- print("The expression ab + bc + cd gets its maximum value for: ")
- print("a = " + str(maxA) + ", b = " + str(maxB) + ", c = " + str(c) + ", d = " + str(d))
- print("Max expression = " + str(maxExpression))
- print("Execution time = " + str(round(elapsed, 2)) + " seconds.")
- print()
- # MAIN FUNCTION
- SUM = list(range(5, 70, 5))
- for sum in SUM:
- print("************************************************")
- maximize(sum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement