Given Nth integer and you have to find the sum of first Nth integers whose digit sum is divisible by 10.
Examples :
Input : 2 Output : 47 Input : 11 Output : 722
Approach :
To find the sum of first Nth integers whose digit sum is divisible by 10.
1. Every 100th number contain 10 such numbers except 0-99 contains only 9 such number and(100-199 contains = 109,118,127,136,145,154,163,172,181 and so on).
2.Do the following step for 1 to N number and calculate the sum
(i) Find the digit sum of given number=S.
(ii) Find the number X which is multiple of 10 and nearest greater than S.
(iii) Calculate the A= X-S.
(iv) Since every 100th number contain 10 such numbers except 1st hundred.So Required number will be i*10+A.
N=2
Sum=0
for i=1
(i) S=1
(ii) X=10
(iii) A=10-1=9
(iv) Ans=1*10+9=19
Sum=19
for i=2
(i) S=2
(ii) X=10
(iii) A=10-2=8
(iv) Ans=2*10+8=28
Sum=47
Final Sum is 47
Implementation :
# Python 3 implementation of
# above approach
#Function to find the digitsum
def getSum(N):
sum = 0
while(N > 0):
sum += N%10
N =N//10
return sum
#function to find the sum of
#first Nth integers whose digit
#sum is divisible by 10.
def solve(N,summ):
for i in range(1,N+1):
S=getSum(i) #return the digit sum
# finding approx number which divisible by 10
x=S//10
y=S%10
if y!=0:
y=1
x=(x+y)*10
# difference in actual value and
#calculated value( value to be added)
a=x-S
summ+=i*10+a
return summ
# Driver Code
if __name__ == "__main__" :
# sample input
N=11
summ=0
summ=solve(N,summ)
print(summ)
Output :
722