Tous les défis
Facile 0 résolus 0 % de réussite

Digit Sum of a Power of Two

Add up the decimal digits of 2 raised to the power n.

Ce que vous allez apprendre

  • Double a number stored as digits
  • Sum the digits of a large number
  • Handle the n = 0 case

Problème

Afficher la traduction

Powers of two grow fast: 2^64 already has 20 digits and 2^2000 has over 600. Compute 2^n exactly and add up its decimal digits. Without big integers, keep the number as an array of digits and double it n times.

Implement digit_sum_power_of_two(n).

Input

n: The exponent.

Output

Return the sum of the decimal digits of 2^n.

Contraintes

0 ≤ n ≤ 2000

Exemples

Exemple 1
Entrée :
n = 10

Sortie attendue :
expected = 7
Entrée :
long long n = 10;

Sortie attendue :
long long expected = 7;
Entrée :
long n = 10;

Sortie attendue :
long expected = 7;

Explication

2^10 = 1024 and 1 + 0 + 2 + 4 = 7.

Exemple 2
Entrée :
n = 0

Sortie attendue :
expected = 1
Entrée :
long long n = 0;

Sortie attendue :
long long expected = 1;
Entrée :
long n = 0;

Sortie attendue :
long expected = 1;

Explication

2^0 = 1, so the digit sum is 1.

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

2^n is 1 doubled n times.

Indice 2

Store digits with the lowest digit first so carries move toward the end of the array.

Indice 3

Add the digits only once the doubling is finished.

Complexité attendue

O(n²) time, O(n) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.