Sum Squared Digits Function
This commit is contained in:
29
sumsquareddigits.py
Normal file
29
sumsquareddigits.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
DIGS = "0123456789ABCDEFGH"
|
||||||
|
|
||||||
|
|
||||||
|
def int2base(n, base):
|
||||||
|
if n == 0:
|
||||||
|
return DIGS[0]
|
||||||
|
|
||||||
|
digits = []
|
||||||
|
while n:
|
||||||
|
digits.append(DIGS[n % base])
|
||||||
|
n = n // base
|
||||||
|
|
||||||
|
digits.reverse()
|
||||||
|
return "".join(digits)
|
||||||
|
|
||||||
|
|
||||||
|
def SSDN(b, n):
|
||||||
|
n = int2base(n, b)
|
||||||
|
ans = 0
|
||||||
|
for x in n:
|
||||||
|
ans += DIGS.index(x) ** 2
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
p = int(input())
|
||||||
|
|
||||||
|
for _ in range(p):
|
||||||
|
k, b, n = map(int, input().split())
|
||||||
|
print(f"{k} {SSDN(b, n)}")
|
||||||
Reference in New Issue
Block a user