Find the number of 10-digit numbers where the sum of the digits is divisible by 5.
%%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
Find the number of 10-digit numbers where the sum of the digits is divisible by 5.
The first digit cannot be zero, so 9 ways, the next 8 digits can be anything, so there are 10 ways for each of them.
We can then only choose two digits as last digit to satisfy the condition.
sum = \(9\times 10^8 \times 2 = 1~ 800~ 000~ 000\)
\(\begin{array}{|c|c|} \hline \text{the sum of the digits 1 until 9} & \text{last digit to satisfy the condition }\\ \hline \ldots 0 & 0~ \text{ or } ~5 \\ \ldots 1 & 4~ \text{ or } ~9 \\ \ldots 2 & 3~ \text{ or } ~8 \\ \ldots 3 & 2~ \text{ or } ~7 \\ \ldots 4 & 1~ \text{ or } ~6 \\ \ldots 5 & 0~ \text{ or } ~5 \\ \ldots 6 & 4~ \text{ or } ~9 \\ \ldots 7 & 3~ \text{ or } ~8 \\ \ldots 8 & 2~ \text{ or } ~7 \\ \ldots 9 & 1~ \text{ or } ~6 \\ \hline \end{array} \)
Example: 1 000 000 08(1) or 1 000 000 08(6)
4 567 123 98(0) or 4 567 123 98(5)