Dado un número entero N , la tarea es encontrar la suma de los dígitos del número N escrito en todas las bases de 2 a N/2 .
Ejemplos:
Entrada: N = 6
Salida: 4
En base 2, 6 se representa como 110.
En base 3, 6 se representa como 20.
Suma = 1 + 1 + 0 + 2 + 0 = 4
Entrada: N = 8
Salida: 7
Acercarse:
- Para cada base de 2 a (n/2) calcule los dígitos de n en la base particular con lo siguiente:
- Calcula el resto de dividir n por base y el resto es una de las cifras de n en esa base.
- Agregue el dígito a la suma y actualice n como (n = n / base) .
- Repita los pasos anteriores mientras n > 0
- Imprime la suma calculada en los pasos anteriores.
A continuación se muestra la implementación del enfoque anterior:
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>
Producción:
7
Complejidad de tiempo: O(n * log n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA