 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find sum of digits in base K using Python
Suppose we have a number n in decimal number system (base 10) have another value k, we have to find the sum of the digits of n after converting given number n from base 10 to base k. When we calculate digit sum, we will consider each digit as decimal (base 10) number.
So, if the input is like n = 985 k = 8, then the output will be 12 because the number 985 in octal is 1731, so the digit sum is 1+7+3+1 = 12.
To solve this, we will follow these steps −
- ans := 0 
-  while n >= k, do - ans := ans + n mod k 
- n := quotient of n/k 
 
- ans := ans + n 
- return ans 
Let us see the following implementation to get better understanding −
Example
def solve(n, k): ans = 0 while n>=k: ans = ans + n%k n = n//k ans = ans+n return ans n = 985 k = 8 print(solve(n, k))
Input
985,8
Output
True
Advertisements
 