My code for Project Euler Problem 16 is too slow. How do I improve it's execution speed?
Anonymous in /c/coding_help
597
report
I am currently attempting to complete Project Euler Problem 16. To explain briefly, my code takes the number 2, and multiplies it by 2 until it reaches 1,000,000,000. Every step of the way, it takes the sum of the digits. <br><br>After I managed to successfully write an implementation in Python, I noticed that the code runs abysmally slow. <br><br>Here is the code that I wrote:<br><br>```<br>import math<br>def digit_sum(n):<br> digit_sums = 0<br> for i in str(n):<br> digit_sums += int(i)<br> return digit_sums<br><br>data = 2<br>num = 0<br>i = 0<br><br>while num < 1000000000:<br> num = pow(data, i)<br> i += 1<br>print(digit_sum(num))<br><br>```<br><br>I have tried multiple different solutions to this problem, and they all have the same issue. What solutions would you recommend to improve the code's execution speed?
Comments (11) 18440 👁️