%%time
# Efficient Python 3 program to sum up the #
# multiples of digit n in a range of 2 - 10-digit integers #
# E.G. "What is the total sum of all 3-digit integers that are
# mutiples of 7?". Answer =70,336 #
# find the Sum of having n digit
# and divisible by the number
def totalSumDivisibleByNum(digit, number):
# compute the first and last term
firstnum = pow(10, digit - 1)
lastnum = pow(10, digit)
# first number which is divisible
# by given number
firstnum = (firstnum - firstnum % number)+number
# last number which is divisible
# by given number
lastnum = (lastnum - lastnum % number )
# total divisible number
count =int ((lastnum - firstnum) / number+1 )
print("Total count =", f"{count:,d}")
# return the total sum
return int(((lastnum + firstnum) * count) / 2)
# Driver code
digit =10 ; num = 5
print("Total Sum =", f"{totalSumDivisibleByNum(digit, num):,d}")
Total count = 1,800,000,000
Total Sum = 9,900,000,002,700,000,000
Wall time: 0 ns