logo

Trobar la suma de dígits d'un nombre fins que la suma es converteixi en un dígit

Prova-ho a GfG Practice ' title=

Donat un nombre enter n hem de trobar repetidament la suma dels seus dígits fins que el resultat es converteixi en un nombre d'una sola xifra.

Exemples:

Entrada: n = 1234
Sortida: 1
Explicació:
Pas 1: 1 + 2 + 3 + 4 = 10
Pas 2: 1 + 0 = 1



Entrada: n = 5674
Sortida: 4
Explicació:
Pas 1: 5 + 6 + 7 + 4 = 22
Pas 2: 2 + 2 = 4

Taula de continguts

[Enfocament ingenu] Afegint dígits repetidament

L'enfocament se centra en el càlcul del roo digital t d'un nombre que és el resultat de sumar els dígits repetidament fins a obtenir un valor d'un sol dígit. A continuació es mostra com funciona conceptualment:



  1. Suma els dígits : Comença afegint tots els dígits del número donat.
  2. Comproveu el resultat : Si la suma és un nombre d'un sol dígit (és a dir, menys de 10), atureu-lo i torneu-lo.
  3. Repetiu el procés : Si la suma encara és més d'un dígit, repetiu el procés amb la suma de dígits. Això continua fins que s'arriba a una suma d'un sol dígit.
C++
// C++ program to find the digit sum by  // repetitively Adding its digits #include    using namespace std; int singleDigit(int n) {  int sum = 0;  // Repetitively calculate sum until  // it becomes single digit  while (n > 0 || sum > 9) {  // If n becomes 0 reset it to sum   // and start a new iteration.  if (n == 0) {  n = sum;  sum = 0;  }  sum += n % 10;  n /= 10;  }  return sum; } int main() {  int n = 1234;  cout << singleDigit(n);  return 0; } 
C
// C program to find the digit sum by  // repetitively Adding its digits #include  int singleDigit(int n) {  int sum = 0;  // Repetitively calculate sum until  // it becomes single digit  while (n > 0 || sum > 9) {  // If n becomes 0 reset it to sum   // and start a new iteration.  if (n == 0) {  n = sum;  sum = 0;  }  sum += n % 10;  n /= 10;  }  return sum; } int main() {  int n = 1234;  printf('%d' singleDigit(n));  return 0; } 
Java
// Java program to find the digit sum by  // repetitively Adding its digits class GfG {  static int singleDigit(int n) {  int sum = 0;  // Repetitively calculate sum until  // it becomes single digit  while (n > 0 || sum > 9) {  // If n becomes 0 reset it to sum   // and start a new iteration.  if (n == 0) {  n = sum;  sum = 0;  }  sum += n % 10;  n /= 10;  }  return sum;  }  public static void main(String[] args) {  int n = 1234;  System.out.println(singleDigit(n));  } } 
Python
# Python program to find the digit sum by  # repetitively Adding its digits def singleDigit(n): sum = 0 # Repetitively calculate sum until # it becomes single digit while n > 0 or sum > 9: # If n becomes 0 reset it to sum  # and start a new iteration if n == 0: n = sum sum = 0 sum += n % 10 n //= 10 return sum if __name__ == '__main__': n = 1234 print(singleDigit(n)) 
C#
// C# program to find the digit sum by  // repetitively Adding its digits using System; class GfG {  static int singleDigit(int n) {  int sum = 0;  // Repetitively calculate sum until  // it becomes single digit  while (n > 0 || sum > 9) {  // If n becomes 0 reset it to sum   // and start a new iteration.  if (n == 0) {  n = sum;  sum = 0;  }  sum += n % 10;  n /= 10;  }  return sum;  }  static void Main() {  int n = 1234;  Console.WriteLine(singleDigit(n));  } } 
JavaScript
// JavaScript program to find the digit sum by  // repetitively Adding its digits function singleDigit(n) {  let sum = 0;  // Repetitively calculate sum until  // it becomes single digit  while (n > 0 || sum > 9) {  // If n becomes 0 reset it to sum   // and start a new iteration.  if (n === 0) {  n = sum;  sum = 0;  }  sum += n % 10;  n = Math.floor(n / 10);  }  return sum; } // Driver Code const n = 1234; console.log(singleDigit(n)); 

Sortida
1

Complexitat temporal: O (log10n) mentre anem iterant sobre els dígits del nombre.
Espai auxiliar: O(1)

[Enfocament esperat] Ús de fórmula matemàtica

Sabem que cada nombre del sistema decimal es pot expressar com una suma de les seves xifres multiplicades per potències de 10. Per exemple, un nombre representat com abcd es pot escriure de la següent manera:

abcd = a*10^3 + b*10^2 + c*10^1 + d*10^0

Podem separar els dígits i reescriure això com:
abcd = a + b + c + d + (a*999 + b*99 + c*9)
abcd = a + b + c + d + 9*(a*111 + b*11 + c)

Això implica que qualsevol nombre es pot expressar com la suma de les seves xifres més un múltiple de 9.
Així que si prenem mòdul amb 9 a cada costat
abcd % 9 = (a + b + c + d) % 9 + 0

Això vol dir que la resta quan es divideix abcd per 9 és igual a la resta on la suma dels seus dígits (a + b + c + d) es divideix per 9.



Si la suma de les xifres en si consta de més d'una xifra, podem expressar aquesta suma com la suma de les seves xifres més un múltiple de 9. En conseqüència, prendre mòdul 9 eliminarà el múltiple de 9 fins que la suma de dígits es converteixi en un número d'un sol dígit.

Com a resultat, la suma dels dígits de qualsevol nombre serà igual al seu mòdul 9. Si el resultat de l'operació mòdul és zero, indica que el resultat d'un sol dígit és 9.
Per saber sobre la implementació del codi Consulteu Arrel digital (suma digital repetida) de l'enter gran donat