W
W
weranda2016-04-28 12:38:58
Python
weranda, 2016-04-28 12:38:58

How to solve a seemingly simple problem in python3?

Greetings
I came across a problem and I can not solve it in accordance with the conditions.
Task:

Write a program that reads two integers a and b and outputs the largest of them. Numbers are integers from 1 to 1000.
When solving the problem, you can use only integer arithmetic operations +, -, *, //, %, =. You can not use non-linear constructions: branches, loops, functions, methods.

It follows from the bold text that only arithmetic operators can be used.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vladimir Kuts, 2016-04-28
@weranda

For example offhand:

>>> a,b=94,54
>>> (a*(a//b) + b*(b//a))/(b//a+a//b)
94
>>> a,b=6,322
>>> (a*(a//b) + b*(b//a))/(b//a+a//b)
322

PS I did it in the Python console, so I didn’t explicitly use functions for input / output to the screen, if it’s important, of course :)

A
aRegius, 2016-04-28
@aRegius

Hello.
1. There are two numbers, x and y (positive integers).
2. There is their difference, z ( z = x - y )
3. To find the largest of them, all you need to do is sum these two numbers and divide their differences by 2 :
4. The only "ambush" is 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, is positive.
6. For this we have this very differencewe raise it to the second power and extract the square root from the result - in order for the code to be less noisy, we divide this procedure into two stages: 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

A
Alexey, 2016-04-28
@RusTech

print((((a // b) * a) + ((b // a) * b)) // ((a // b) + (b // a)))

V
vatrusheknet, 2019-11-18
@vatrusheknet

A = int(input())
B = int(input())
A1 = A // B
A2 = A % B
A3 = 0**(A - A2)
B1 = B // A
B2 = B % A
B3 = 0**(B - B2)
Max = A1 * B2 + A2 * B1 + B2 * A3 + A2 * B3 + A * A3**B3 * B3**A3
print(Max)

R
Raffon, 2021-12-26
@Raffon

print((a + b + abs(a - b)) / 2)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question