0

Below are two code:

First:

def sofd(num):
    summ=0
    while num:
        summ=summ+num%10
        num//=10
    return summ
limit=10**7
total=0
for i in range(1,limit):total+=sofd(i)
print(total)

Second:

total=0
limit=10**6
for num in range(1,limit):
    while num:
        total+=num%10
        num//=10       
print(total)

Why the first code is around 60% faster than second code?
I am new to python so please pardon my ignorance if this question is very trivial.
I am running both on Jupyter Notebook.

0 Answers0