How to solve a seemingly simple problem on python3?


Came across a problem and unable to solve it in accordance with the conditions.


Write a program that reads two integers a and b, and outputs the maximum value from them. Integers from 1 to 1000.

When solving a problem, you can use only integer arithmetic operations +, -, *, //, %, =. Do not use non-linear structures: branches, loops, functions, methods.

From bold the text that are only arithmetic operators.
July 12th 19 at 17:09
3 answers
July 12th 19 at 17:11
For example, offhand:
>>> a,b=94,54
>>> (a*(a//b) + b*(b//a))/(b//a+a//b)
>>> a,b=6,322
>>> (a*(a//b) + b*(b//a))/(b//a+a//b)

P. S. did in Python console, so clearly the functions for input/output on the screen is not used if it's important, of course :)
Tell us, how did it come to this (in a good way) and that it is possible to esteem on this subject? - reyna_West92 commented on July 12th 19 at 17:14
: Well, actually knowledge of arithmetic operators Python and their own brains enough. Something to read on this subject - I don't know :) - Clarabelle_Brown commented on July 12th 19 at 17:17
But where the same logic should be followed? Trying to comprehend the algorithm of this calculation, but... - reyna_West92 commented on July 12th 19 at 17:20
: The logic is that when an integer division is a smaller number will result in 0. And 0 when multiplied by any number gives 0. In the same way as the addition. Thus, in the numerator, we get the result of integer division, multiplied by the larger number. It remains only to reduce this the result of an integer division. Something like that... - Clarabelle_Brown commented on July 12th 19 at 17:23
July 12th 19 at 17:13

1. There are two numbers x and y (positive integers).
2. Is their difference, z (z = x - y)
3. To find more of them, just need the sum of those two numbers and their difference divided by 2:
max_num = (x + y + z)/2
4. The only "ambush" in that, due to the uncertainty of numbers, their difference can be a negative number (for example, x = 5, y = 853, x - y = - 848).
5. Therefore, we need to ensure that the difference, in any case, were positive.
6. To do this, we this very difference constructed in the second degree and recoverable from the result of the square root in order to make the code less noisy, divide this procedure into two steps: a) z = (x - y) ** 2; b) z = z ** .5

7. Final code:
z = (x - y) ** 2
z = z ** .5
max_num = (x + y + z) / 2
I apologize for necroposting, but there is not a single "ambush". The second "ambush" that ** .5 - this is a feature of Python, most other languages the square root is, after all, a function. If (x-y) ** 2 can be rewritten as (x-y)*(x-y) then square root to represent a simple arithmetic operation will not work. Besides, the TS clearly indicated a list of operators with whom you can work: +, -, *, //, %, = - reyna_West92 commented on July 12th 19 at 17:16
: Brought You a novel... things of bygone days... :) Then do you need these two steps to remove, and the difference of the numbers just to pass the argument to abs z = abs(x - y). - Clarabelle_Brown commented on July 12th 19 at 17:19
July 12th 19 at 17:15
print((((a // b) * a) + ((b // a) * b)) // ((a // b) + (b // a)))

Find more questions by tags Python