|
N = int(input()) x = 0 result = 1 while result * 2 <= N: result *= 2 x += 1 print(x) print(result) |
Explanation of the Code
- Reading Input:
- The
input()
function reads a line of text from the user.
- The
int()
function converts this input to an integer and stores it in the variable N
.
- Finding the Greatest Exponent:
x = 0
result = 1
while result * 2 <= N:
result *= 2
x += 1
- Initialize
x
to 0 and result
to 1 (since 20=12^0 = 120=1).
- Use a
while
loop to keep doubling the result
(i.e., multiplying result
by 2) until result * 2
exceeds N
.
- Each time the loop doubles
result
, increment x
by 1.
- Printing the Exponent and Result:
- Once the loop finds the greatest
result
less than or equal to N
, print the value of x
and result
.