Tuesday, November 19, 2024
Google search engine
HomeData Modelling & AISum of the digits of a number N written in all bases...

Sum of the digits of a number N written in all bases from 2 to N/2

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:
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:
 

 

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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the sum of the digits of
// n in the given base
int solve(int n, int base)
{
    // Sum of digits
    int sum = 0;
 
    while (n > 0) {
 
        // Digit of n in the given base
        int remainder = n % base;
 
        // Add the digit
        sum += remainder;
        n = n / base;
    }
 
    return sum;
}
 
// Function to calculate the sum of
// digits of n in bases from 2 to n/2
void SumsOfDigits(int n)
{
    // to store digit sum in all bases
    int sum = 0;
 
    // function call for multiple bases
    for (int base = 2; base <= n / 2; ++base)
        sum += solve(n, base);
 
    cout << sum;
}
 
// Driver program
int main()
{
    int n = 8;
    SumsOfDigits(n);
    return 0;
}


Java




// Java implementation of the approach
 
import java.io.*;
 
class GFG {
     
 
// Function to calculate the sum of the digits of
// n in the given base
static int solve(int n, int base)
{
    // Sum of digits
    int sum = 0;
 
    while (n > 0) {
 
        // Digit of n in the given base
        int remainder = n % base;
 
        // Add the digit
        sum += remainder;
        n = n / base;
    }
 
    return sum;
}
 
// Function to calculate the sum of
// digits of n in bases from 2 to n/2
static void SumsOfDigits(int n)
{
    // to store digit sum in all bases
    int sum = 0;
 
    // function call for multiple bases
    for (int base = 2; base <= n / 2; ++base)
        sum += solve(n, base);
 
    System.out.println(sum);
}
 
// Driver program
 
 
    public static void main (String[] args) {
        int n = 8;
    SumsOfDigits(n);
    }
     
}
// This code is contributed by anuj_67..


Python3




# Python 3 implementation of the approach
from math import floor
# Function to calculate the sum of the digits of
# n in the given base
def solve(n, base):
    # Sum of digits
    sum = 0
 
    while (n > 0):
        # Digit of n in the given base
        remainder = n % base
 
        # Add the digit
        sum = sum + remainder
        n = int(n / base)
     
    return sum
 
# Function to calculate the sum of
# digits of n in bases from 2 to n/2
def SumsOfDigits(n):
     
    # to store digit sum in all base
    sum = 0
    N = floor(n/2)
    # function call for multiple bases
    for base in range(2,N+1,1):
        sum = sum + solve(n, base)
 
    print(sum)
 
# Driver program
if __name__ == '__main__':
    n = 8
    SumsOfDigits(n)
     
# This code is contributed by
# Surendra_Gangwar


C#




// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to calculate the sum of
// the digits of n in the given base
static int solve(int n, int base1)
{
    // Sum of digits
    int sum = 0;
 
    while (n > 0)
    {
 
        // Digit of n in the given base
        int remainder1 = n % base1;
 
        // Add the digit
        sum += remainder1;
        n = n / base1;
    }
 
    return sum;
}
 
// Function to calculate the sum of
// digits of n in base1s from 2 to n/2
static void SumsOfDigits(int n)
{
    // to store digit sum in all bases
    int sum = 0;
 
    // function call for multiple bases
    for (int base1 = 2;
             base1 <= n / 2; ++base1)
        sum += solve(n, base1);
 
    Console.WriteLine(sum);
}
 
// Driver Code
public static void Main (String []args)
{
    int n = 8;
    SumsOfDigits(n);
}
}
 
// This code is contributed by Arnab Kundu


PHP




<?php
// PHP implementation of the approach
 
// Function to calculate the sum of
// the digits of n in the given base
function solve($n, $base)
{
    // Sum of digits
    $sum = 0;
 
    while ($n > 0)
    {
 
        // Digit of n in the given base
        $remainder = $n % $base;
 
        // Add the digit
        $sum += $remainder;
        $n = $n / $base;
    }
 
    return $sum;
}
 
// Function to calculate the sum of
// digits of n in bases from 2 to n/2
function SumsOfDigits($n)
{
    // to store digit sum in all bases
    $sum = 0;
 
    // function call for multiple bases
    for ($base = 2;
         $base <= $n / 2; ++$base)
        $sum += solve($n, $base);
 
    echo $sum;
}
 
// Driver Code
$n = 8;
SumsOfDigits($n);
 
// This code is contributed
// by Akanksha Rai
?>


Javascript




<script>
// javascript implementation of the approach   
// Function to calculate the sum of the digits of
    // n in the given base
    function solve(n , base)
    {
     
        // Sum of digits
        var sum = 0;
 
        while (n > 0) {
 
            // Digit of n in the given base
            var remainder = n % base;
 
            // Add the digit
            sum += remainder;
            n = parseInt(n / base);
        }
 
        return sum;
    }
 
    // Function to calculate the sum of
    // digits of n in bases from 2 to n/2
    function SumsOfDigits(n)
    {
     
        // to store digit sum in all bases
        var sum = 0;
 
        // function call for multiple bases
        for (base = 2; base <= n / 2; ++base)
            sum += solve(n, base);
 
        document.write(sum);
    }
 
    // Driver program
        var n = 8;
        SumsOfDigits(n);
 
// This code is contributed by gauravrajput1
</script>


Output: 

7

 

Time Complexity: O(n * log n)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments