Sum of the digits of a number N written in all bases from 2 to N/2
Last Updated :
08 Jul, 2022
Given an integer N, the task is to find the sum of the digits of the number N written in all the bases from 2 to N / 2.
Examples:
Input: N = 6
Output: 4
In base 2, 6 is represented as 110.
In base 3, 6 is represented as 20.
Sum = 1 + 1 + 0 + 2 + 0 = 4
Input: N = 8
Output: 7
Approach:
- For every base from 2 to (n / 2) calculate the digits of n in the particular base with the following:
- Calculate the remainder on dividing n by base and the remainder is one of the digits of n in that base.
- Add the digit to the sum and update n as (n = n / base).
- Repeat the above steps while n > 0
- Print the sum calculated in the previous steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int n, int base)
{
int sum = 0;
while (n > 0) {
int remainder = n % base;
sum += remainder;
n = n / base;
}
return sum;
}
void SumsOfDigits( int n)
{
int sum = 0;
for ( int base = 2; base <= n / 2; ++base)
sum += solve(n, base);
cout << sum;
}
int main()
{
int n = 8;
SumsOfDigits(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int solve( int n, int base)
{
int sum = 0 ;
while (n > 0 ) {
int remainder = n % base;
sum += remainder;
n = n / base;
}
return sum;
}
static void SumsOfDigits( int n)
{
int sum = 0 ;
for ( int base = 2 ; base <= n / 2 ; ++base)
sum += solve(n, base);
System.out.println(sum);
}
public static void main (String[] args) {
int n = 8 ;
SumsOfDigits(n);
}
}
|
Python3
from math import floor
def solve(n, base):
sum = 0
while (n > 0 ):
remainder = n % base
sum = sum + remainder
n = int (n / base)
return sum
def SumsOfDigits(n):
sum = 0
N = floor(n / 2 )
for base in range ( 2 ,N + 1 , 1 ):
sum = sum + solve(n, base)
print ( sum )
if __name__ = = '__main__' :
n = 8
SumsOfDigits(n)
|
C#
using System;
class GFG
{
static int solve( int n, int base1)
{
int sum = 0;
while (n > 0)
{
int remainder1 = n % base1;
sum += remainder1;
n = n / base1;
}
return sum;
}
static void SumsOfDigits( int n)
{
int sum = 0;
for ( int base1 = 2;
base1 <= n / 2; ++base1)
sum += solve(n, base1);
Console.WriteLine(sum);
}
public static void Main (String []args)
{
int n = 8;
SumsOfDigits(n);
}
}
|
PHP
<?php
function solve( $n , $base )
{
$sum = 0;
while ( $n > 0)
{
$remainder = $n % $base ;
$sum += $remainder ;
$n = $n / $base ;
}
return $sum ;
}
function SumsOfDigits( $n )
{
$sum = 0;
for ( $base = 2;
$base <= $n / 2; ++ $base )
$sum += solve( $n , $base );
echo $sum ;
}
$n = 8;
SumsOfDigits( $n );
?>
|
Javascript
<script>
function solve(n , base)
{
var sum = 0;
while (n > 0) {
var remainder = n % base;
sum += remainder;
n = parseInt(n / base);
}
return sum;
}
function SumsOfDigits(n)
{
var sum = 0;
for (base = 2; base <= n / 2; ++base)
sum += solve(n, base);
document.write(sum);
}
var n = 8;
SumsOfDigits(n);
</script>
|
Time Complexity: O(n * log n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...