Looking for insight into how to optimize the speed of a Python script.
Challenge: "Which positive integers are equal to 23 times the sum of their digits" >> this is my functioning code:
def sum_of_digits(number):
sum = 0;
for c in number:
sum += ord(c) - 48;
return sum;
upper_limit = 10000000000;
count = 0;
for i in range(1, upper_limit, 1):
if(23 * sum_of_digits(str(i)) == i):
print(i);
count += 1;
Which correctly spits out 207 as an answer (2+0+7 = 9. 9 x 23 = 207).
Problem is the time for execution scales as (n).
Whilst I can pre calculate the sum_of_digits
- this wont save time and am looking for Python methods to speed to execution.
I know I can delve into number theory and "maths" it - but am interested in what I can squeeze out of Python.
Finally, I know other languages might be quicker, but I'm a high school physics teacher using this as a STEM example in a summer class.