0
How do i make the following equasion work in python?; 2^x<input<2^(x+1)
12 Respuestas
+ 2
pixel strength use a while loop.
You keep divide the number by 2, untill it is less than 1, and make a counter to count the number of divisions.
When that condition is true, the counter will take the value of x+1 (because it will be already beggier than 2^x), so then you just assign counter-1 to x.
number = int(input())
counter = 0
while number<1:
number/=2
counter+=1
x = counter-1
One last thing to do is to check if number==1, if it is, then the input is equal exactly to 2^(x+1), and not just inferior than it
+ 1
What have you tried ?
+ 1
pixel strength according to what you just said, you want your number to be different than all 2^x
which means it has to be different than 1, 2, 4, 8, 16... is this what you want ?
+ 1
pixel strength So your objective is to get the value of x ?
+ 1
I think you could just use log2 function then round its value:
x1 = math.floor(math.log2(input)) #first value
x2 = math.ceil(math.log2(input)) #another one
So equation 2^x1 < input < 2^x2 is valid
Use this link for reference
https://docs.python.org/3.3/library/math.html
https://www.tutorialgateway.org/python-log2/
Don't forget to import math first
+ 1
pascal aymane boukrouh helped me fix it last night but thank you for the help anyway. This is the final code. https://code.sololearn.com/cS096a5y9Cav/?ref=app
0
My main problem with this is that x needs to equal all posative integers Aymane Boukrouh [Unavailable]
0
2**x< input < 2**(x+1)
Example;
Input:17
2^4 < 17 < 2^5
Or;
Input 36
2^5 < 36 < 2^6
I want the code to automattically figure out which number to put 2 to the power of by this type of logic and testing. Maybe a simple ai is needed? Aymane Boukrouh [Unavailable]
0
I will also have a check for if (input = 2**x) Aymane Boukrouh [Unavailable]
0
Yes that is basically what i want. Sorry for so many notifications Aymane Boukrouh [Unavailable]
0
For it to try different values of x until the inequality statement is true Aymane Boukrouh [Unavailable]
0
I messaged you personally Aymane Boukrouh [Unavailable]