Suma de dígitos escritos en diferentes bases de 2 a n-1

Dado un número n, encuentre la suma de los dígitos de n cuando se representa en diferentes bases de 2 a n-1.
Ejemplos: 
 

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
  1. Como la pregunta dada quiere la suma de dígitos en bases diferentes, primero tenemos que calcular el número dado de bases diferentes y sumar cada dígito al número de bases diferentes.
  2. Entonces, para calcular la representación de cada número tomaremos la mod del número dado por la base en la que queremos representar ese número.
  3. Luego, tenemos que sumar todos esos valores mod ya que los valores mod obtenidos representarán ese número en esa base.
  4. Finalmente, la suma de esos valores mod da la suma de los dígitos de ese número.

A continuación se muestran las implementaciones de este enfoque.
 

C++

// CPP program to find sum of digits of
// n in different bases from 2 to n-1.
#include <bits/stdc++.h>
using namespace std;
 
// function to calculate sum of
// digit for a given base
int solve(int n, int base)
{
    // Sum of digits
    int result = 0 ;
     
    // Calculating the number (n) by
    // taking mod with the base and adding
    // remainder to the result and
    // parallelly reducing the num value .
    while (n > 0)
    {
        int remainder = n % base ;
        result = result + remainder ;
        n = n / base;
    }
     
    // returning the result
    return result ;
}
 
void printSumsOfDigits(int n)
{
    // function calling for multiple bases
    for (int base = 2 ; base < n ; ++base)   
        cout << solve(n, base) <<" ";
}
 
// Driver program
int main()
{
    int n = 8;
    printSumsOfDigits(n);
    return 0;
}

Java

// Java program to find sum of digits of
// n in different bases from 2 to n-1.
class GFG
{
// function to calculate sum of
// digit for a given base
static int solve(int n, int base)
{
    // Sum of digits
    int result = 0 ;
     
    // Calculating the number (n) by
    // taking mod with the base and adding
    // remainder to the result and
    // parallelly reducing the num value .
    while (n > 0)
    {
        int remainder = n % base ;
        result = result + remainder ;
        n = n / base;
    }
     
    // returning the result
    return result ;
}
 
static void printSumsOfDigits(int n)
{
    // function calling for multiple bases
    for (int base = 2 ; base < n ; ++base)
        System.out.print(solve(n, base)+" ");
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 8;
    printSumsOfDigits(n);
}
}
// This code is contributed by Smitha

Python3

# Python program to find sum of digits of
# n in different bases from 2 to n-1.
  
# def to calculate sum of
# digit for a given base
def solve(n, base) :
      
    # Sum of digits
    result = 0
      
    # Calculating the number (n) by
    # taking mod with the base and adding
    # remainder to the result and
    # parallelly reducing the num value .
    while (n > 0) :
     
        remainder = n % base
        result = result + remainder 
        n = int(n / base)
      
    # returning the result
    return result
  
def printSumsOfDigits(n) :
      
    # def calling for
    # multiple bases
    for base in range(2, n) :
        print (solve(n, base), end=" ")
 
# Driver code
n = 8
printSumsOfDigits(n)
  
# This code is contributed by Manish Shaw
# (manishshaw1)

C#

// Java program to find the sum of digits of
// n in different base1s from 2 to n-1.
using System;
 
class GFG
{
// function to calculate sum of
// digit for a given base1
static int solve(int n, int base1)
{
    // Sum of digits
    int result = 0 ;
     
    // Calculating the number (n) by
    // taking mod with the base1 and adding
    // remainder to the result and
    // parallelly reducing the num value .
    while (n > 0)
    {
        int remainder = n % base1 ;
        result = result + remainder ;
        n = n / base1;
    }
     
    // returning the result
    return result ;
}
 
static void printSumsOfDigits(int n)
{
    // function calling for multiple base1s
    for (int base1 = 2 ; base1 < n ; ++base1)
        Console.Write(solve(n, base1)+" ");
}
 
// Driver Code
public static void Main()
{
    int n = 8;
    printSumsOfDigits(n);
}
}
// This code is contributed by Smitha

PHP

<?php
// PHP program to find sum of digits of
// n in different bases from 2 to n-1.
 
// function to calculate sum of
// digit for a given base
function solve($n, $base)
{
     
    // Sum of digits
    $result = 0 ;
     
    // Calculating the number (n) by
    // taking mod with the base and adding
    // remainder to the result and
    // parallelly reducing the num value .
    while ($n > 0)
    {
        $remainder = $n % $base ;
        $result = $result + $remainder ;
        $n = $n / $base;
    }
     
    // returning the result
    return $result ;
}
 
function printSumsOfDigits($n)
{
     
    // function calling for
    // multiple bases
    for ($base = 2 ; $base < $n ; ++$base)
    {
        echo(solve($n, $base));
        echo(" ");
    }
}
 
// Driver code
$n = 8;
printSumsOfDigits($n);
 
// This code is contributed by Ajit.
?>

Javascript

<script>
 
// JavaScript program to find sum of digits of
// n in different bases from 2 to n-1.
 
    // function to calculate sum of
    // digit for a given base
    function solve(n , base) {
        // Sum of digits
        var result = 0;
 
        // Calculating the number (n) by
        // taking mod with the base and adding
        // remainder to the result and
        // parallelly reducing the num value .
        while (n > 0) {
            var remainder = n % base;
            result = result + remainder;
            n = parseInt(n / base);
        }
 
        // returning the result
        return result;
    }
 
    function printSumsOfDigits(n) {
        // function calling for multiple bases
        for (base = 2; base < n; ++base)
            document.write(solve(n, base) + " ");
    }
 
    // Driver Code
     
        var n = 8;
        printSumsOfDigits(n);
 
// This code contributed by Rajput-Ji
 
</script>

Producción : 

1 4 2 4 3 2 

Publicación traducida automáticamente

Artículo escrito por Surya Priy y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *