Sum of digits written in different bases from 2 to n-1
Given a number n, find the sum of digits of n when represented in different bases from 2 to n-1.
Examples:
Input : 5
Output : 2 3 2
Representation of 5 is 101, 12, 11 in bases 2 , 3 , 4 .
Input : 7
Output : 3 3 4 3 2
- As the given question wants the sum of digits in different bases, first we have to calculate the given number of different bases and add each digit to the number of different bases.
- So, to calculate each number’s representation we will take the mod of given number by the base in which we want to represent that number.
- Then, we have to add all those mod values as the mod values obtained will represent that number in that base.
- Finally, the sum of those mod values gives the sum of digits of that number.
Below are implementations of this approach
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int n, int base)
{
int result = 0 ;
while (n > 0)
{
int remainder = n % base ;
result = result + remainder ;
n = n / base;
}
return result ;
}
void printSumsOfDigits( int n)
{
for ( int base = 2 ; base < n ; ++base)
cout << solve(n, base) << " " ;
}
int main()
{
int n = 8;
printSumsOfDigits(n);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int solve( int n, int base)
{
int result = 0 ;
while (n > 0 )
{
int remainder = n % base ;
result = result + remainder ;
n = n / base;
}
return result ;
}
static void printSumsOfDigits( int n)
{
for ( int base = 2 ; base < n ; ++base)
System.out.print(solve(n, base)+ " " );
}
public static void main(String[] args)
{
int n = 8 ;
printSumsOfDigits(n);
}
}
|
Python3
def solve(n, base) :
result = 0
while (n > 0 ) :
remainder = n % base
result = result + remainder
n = int (n / base)
return result
def printSumsOfDigits(n) :
for base in range ( 2 , n) :
print (solve(n, base), end = " " )
n = 8
printSumsOfDigits(n)
|
C#
using System;
class GFG
{
static int solve( int n, int base1)
{
int result = 0 ;
while (n > 0)
{
int remainder = n % base1 ;
result = result + remainder ;
n = n / base1;
}
return result ;
}
static void printSumsOfDigits( int n)
{
for ( int base1 = 2 ; base1 < n ; ++base1)
Console.Write(solve(n, base1)+ " " );
}
public static void Main()
{
int n = 8;
printSumsOfDigits(n);
}
}
|
PHP
<?php
function solve( $n , $base )
{
$result = 0 ;
while ( $n > 0)
{
$remainder = $n % $base ;
$result = $result + $remainder ;
$n = $n / $base ;
}
return $result ;
}
function printSumsOfDigits( $n )
{
for ( $base = 2 ; $base < $n ; ++ $base )
{
echo (solve( $n , $base ));
echo ( " " );
}
}
$n = 8;
printSumsOfDigits( $n );
?>
|
Javascript
<script>
function solve(n , base) {
var result = 0;
while (n > 0) {
var remainder = n % base;
result = result + remainder;
n = parseInt(n / base);
}
return result;
}
function printSumsOfDigits(n) {
for (base = 2; base < n; ++base)
document.write(solve(n, base) + " " );
}
var n = 8;
printSumsOfDigits(n);
</script>
|
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
As constant extra space is used.
Last Updated :
22 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...