Reputation: 335
Super-sum S of an integer x is defined as x if x is single digit number otherwise Super-sum of x is defined as Super-sum of digit-sum of x. Given two numbers n,k find the Super-sum of the number formed when n is concatenated k times.Note that k is only multiplied with the number when it is at least a 2 digit number
Input: 1987 4
Output: 1
Is there a faster method than this?
s,k=input().split()
summ=0
for ele in s:
summ+=int(ele)
s=summ*int(k)
while s>=10:
s=str(s)
summ=0
for ele in s:
summ+=int(ele)
s=summ
print(s)
Upvotes: 0
Views: 359
Reputation: 14680
The formula used in the accepted answer should actually be proved. So, let's do this.
First, let's prove that for any positive integer number N
, N
modulo 9
is the same sum of its digits modulo 9
.
Having proved the statement above, we easily conclude that N % 9
provides the needed digit as per challenge description. The only exception is if N % 9 == 0
, then a digit is 9
.
Upvotes: 0
Reputation: 749
n,k=map(int,input().split())
if n<10:
print(n)
else:
if ((n*k)%9==0):
print(9)
else:
res=(n*k)%9
Any number greater than 9 will have digits repeated that's why you need to take mod of 9 for example 13 will have sum of 1+3 =4 and 13 %9=4 .There will be a special case when mod of 9 will be zero and this will be at number 9,18,27,36 etc which are divisible by 9 and their sum will always be 9 hence return 9 .
Upvotes: 2