How do i make the following equasion work in python?; 2^x<input<2^(x+1) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do i make the following equasion work in python?; 2^x<input<2^(x+1)

15th Nov 2019, 5:56 PM
pixel strength
pixel strength - avatar
12 Answers
+ 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
15th Nov 2019, 10:24 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
What have you tried ?
15th Nov 2019, 6:01 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 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 ?
15th Nov 2019, 10:09 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
pixel strength So your objective is to get the value of x ?
15th Nov 2019, 10:18 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 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
16th Nov 2019, 2:56 AM
pascal
pascal - avatar
+ 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
16th Nov 2019, 2:02 PM
pixel strength
pixel strength - avatar
0
My main problem with this is that x needs to equal all posative integers Aymane Boukrouh [Unavailable]
15th Nov 2019, 10:06 PM
pixel strength
pixel strength - avatar
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]
15th Nov 2019, 10:12 PM
pixel strength
pixel strength - avatar
0
I will also have a check for if (input = 2**x) Aymane Boukrouh [Unavailable]
15th Nov 2019, 10:13 PM
pixel strength
pixel strength - avatar
0
Yes that is basically what i want. Sorry for so many notifications Aymane Boukrouh [Unavailable]
15th Nov 2019, 10:14 PM
pixel strength
pixel strength - avatar
0
For it to try different values of x until the inequality statement is true Aymane Boukrouh [Unavailable]
15th Nov 2019, 10:19 PM
pixel strength
pixel strength - avatar
0
I messaged you personally Aymane Boukrouh [Unavailable]
15th Nov 2019, 10:31 PM
pixel strength
pixel strength - avatar