how to optimise my code to make execute in<0.612 sec | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 5

how to optimise my code to make execute in<0.612 sec

actually, i have take a test, online. my program is correct and passed all test cases but unfortunately, i couldn't submit my code. the below program is to find the nearest power of number 'a' and the resultant answer should be less than or equal to 'b' example: 2^3=8 #here, a is 2 and b is 8. the logic is below... def nearestPower(a,b): i=int(0) x=int(1) for x in range(int(b/2)): n=pow(a,x) if(pow(a,x)<=b): rem=int(n) return rem. in output box: your program took more time than expected. Expected time limit<0.612 sec. Hint: please optimise your code.. (my opinion: my code took more time due to range() in for loop.) how to optimise my code:

26th Jul 2019, 6:26 AM
Sujithra
11 Respuestas
+ 7
I tried all your opinions but unfortunately nothing worked,and finally i unlocked the solution. this is it. def nearestPower(a,b): x=math.floor(math.log(b,a)) xPlusOne=x+1 number1=a**x number2=a**xPlusOne if(abs(number1-b)>abs(number2-b)): return number2 else: return number1 Thank you for your help and support
26th Jul 2019, 12:13 PM
Sujithra
+ 9
Using your code i did some small changes and compared speed in relation to the original code: def nearestPower(a,b): for x in range(b // 2): n = pow(a, x) if n <= b: return n nearestPower(2,8) speed is: vesion original 0:00:00.638562 revised code 0:00:00.161031
26th Jul 2019, 8:44 AM
Lothar
Lothar - avatar
+ 5
~swim~ It was an contest question while submitting the solution, it calculates the time (built in constraints), by that we wil come to know the time.
26th Jul 2019, 4:43 PM
Sujithra
+ 3
Thank you for all your answers....
26th Jul 2019, 9:21 AM
Sujithra
+ 2
This is my recursive implementation. Can you test to see if it's faster? https://code.sololearn.com/ceia86qzJXLa/?ref=app
26th Jul 2019, 7:03 AM
Trigger
Trigger - avatar
+ 2
Try this, maybe it can perform a little better ... def nearestPower(a, b): c ,p, x = 0, 1, 0 while True: c = pow(a, x) if c > b: return p p = c x += 1
26th Jul 2019, 7:13 AM
Ipang
+ 2
Ipang, I dont think it will. Itll run longer. I think ✨Sujithra💫 should use the for loop, but only up to the square root of b
26th Jul 2019, 7:41 AM
Trigger
Trigger - avatar
+ 2
Thomas Williams Maybe, I just tried it with the example (a=2, b=8), didn't run any benchmark, just a test run on Playground : )
26th Jul 2019, 8:14 AM
Ipang
+ 2
Ipang I have no doubt that itll work. I just think that limiting the loop even more would work better :)
26th Jul 2019, 8:28 AM
Trigger
Trigger - avatar
+ 2
Thomas Williams Yes I understand what you mean. I haven't looked into adding more break conditions into that loop. Thanks for reminding me about it Thomas 👍
26th Jul 2019, 8:33 AM
Ipang
+ 2
The time it takes to execute any code depends on the power of the machine it's being run on..
26th Jul 2019, 11:46 PM
haydenki
haydenki - avatar