
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
Sum of digits written in different bases from 2 to n-1
The problem statement includes printing the sum of digits of N, which will be the user input, when written in different bases from 2 to N?1.
In this problem, we will be provided any positive integer N and we need to represent that number in a different base numeral system from 2 to N?1 and find the sum of the digit of each different base numeral system.
In the base?n numeral system, every digit of the representation of any number in that numeral system from right represents the number of times power of n from 0 to 31.
For example, when 5 is being represented in base?2 numeral system,
It is represented as 101 i.e $\mathrm{2^{2}+2^{0}=5}$
Meanwhile, when 5 is being represented in base?3 numeral system,
It is represented as 12 $\mathrm{3^{1}+2*3^{0}=3+2=5}$
Let's understand the problem with the examples below.
Input
N=6
Output
2 2 3 2
Explanation ? The input given is 6. So, here we need to find the sum of digits of N i.e. 6 when being represented in a different base numeral system from 2 to 5.
When 6 is represented in base?2 numeral system, it is written as 110 i.e.$\mathrm{2^{2}+2^{1}=6}$
The sum of digits of representation of 6 in base?2 is 2
The representation of 6 in base?3 numeral system is 20 i.e $\mathrm{2^{2}*3^{1}=6}$.The sum of digits of 6 in base?3 is 2.
The representation of 6 in base?4 numeral system is 12 i.e.$\mathrm{4^{1}+2*4^{0}=4+2=6}$ The sum of the digits of 6 in base?4 representation is 1+2=3.
The representation of 6 in base?5 numeral system is 11 i.e $\mathrm{5^{1}+5^{0}=6}$. The sum of digits of 6 in the base?5 numeral system is 2.
Hence, our required output is 2 2 3 2.
Input
N=9
Output
2 1 3 5 4 3 2
Explanation ? Since the input is 9, we need to represent 9 in a different base numeral system from 2 to 8 and find the sum of digits of each representation. Therefore,
9 in base?2 : 1001 thus the sum of digits is 2.
9 in base?3 : 100, thus the sum of digits is 1.
9 in base?4 : 21, making the sum of digits 3.
9 in base?5 : 14, making the sum of digits 5.
9 in base?6 : 13, making the sum of digits 4.
9 in base?7 : 12, making the sum of digits 3.
9 in base?8 : 11, making the sum of digits 2.
Hence, our required output is 2 1 3 5 4 3 2.
Let's understand the algorithm to print the sum of digits of a number when represented in a different base numeral system.
Algorithm
As we need to find the sum of digits of representation of a number N for each base from 2 to N?1, we will create a function to find the sum of N for every base representation.
To represent any number N in any base number, we take the mod of the number with the base number which represents each digit from right and then divide the number by the base number until N becomes 0 to get the representation of that number in the particular base numeral system.
For example we need to represent 9 in the base?5 numeral system.
Dividing 9 by 5 gives 4 as a remainder, so the rightmost digit will be 4 and then update N by dividing 9 by 5 which gives 1.
Now, dividing 1 by 5 gives 1 as a remainder, so the next digit to the left of the previous digit will be 1 and then N will be 0.
So the representation of 9 in base?5 numeral system will be 14 which represent $\mathrm{5^{1}+4*5^{0}=9}$
Since we just need the sum of digits of representation of N in different base values, we will simply keep adding remainders of N when divided by base value and update N by dividing it by base value until N is greater than 0 to get the sum of the digits of N when written in different base values.
We will use this algorithm to calculate the sum of digits of N when represented in different base values from 2 to N?1 in our approach in order to solve the problem.
Approach
The steps to follow to implement the above algorithm in our approach:
We will simply make a function to give the sum of digits of N when represented in different base values from 2 to N?1, where the passed parameters will be N and the base number.
We will iterate in a while loop until N is greater than 0 where we will find the remainder of N when divided by base number and add it in the variable to store the sum of digits and then update N by dividing N by base number.
Once N becomes 0, return the sum of digits stored in the variable.
Iterate in a for loop from i=2 to i<=N?1 to find the sum of digits of N when represented in different base numbers, where i represents a particular base number.
We will print the sum of digits of each different bases by calling the function to calculate the sum of digits of N when written in different bases in the for loop for every value of i from 2 to N?1.
Example
// C++ program to print the Sum of digits written in different bases from 2 to N-1 #include<bits/stdc++.h> using namespace std; //function to give the sum of digits of N when represented in a particular base int sumOfDigit(int N,int B) { int sum = 0; //variable to store the sum of digits //iterate until N is greater than zero while(N > 0) { int rem=N % B; //finding the remainder of N when divided by base number sum=sum + rem; //add remainder with sum to store the sum of digits N=N / B; //update N by dividing N by base number } return sum; //return the sum of digits stored in the variable } int main() { int N=10; cout<<"Sum of digits written in different bases from 2 to N-1 : " <<endl; //using for loop from i=2 to i<=N-1 to find the sum of digits of N when represented in different base number for(int i =2; i<=N-1; i++){ //printing the sum of digits of each different bases by calling the function to calculate the sum of digits of N cout<<sumOfDigit(N,i)<<" "; } return 0; }
Output
Sum of digits written in different bases from 2 to N-1 2 2 4 2 5 4 3 2
Time complexity? O(N*logN) , because it took log N time to calculate the sum of digits in different bases.
Space complexity ? O(1) , because no extra space is used in our approach.
Conclusion
The concept of printing the sum of digits of N when represented in different bases from 2 to N?1 was discussed in the article. We used an efficient approach to find the sum of digits of N in different base values in C++ in (log N) runtime and within constant space.
I hope you understand the concept and the solution to the problem after reading this article.