数値の桁数の合計が一桁になるまでの合計を求める

数値の桁数の合計が一桁になるまでの合計を求める
GfG Practice で試してみる

整数 n が与えられた場合、結果が 1 桁の数値になるまで、その桁の合計を繰り返し求める必要があります。

例:

入力: n = 1234
出力: 1
説明:
ステップ 1: 1 + 2 + 3 + 4 = 10
ステップ 2: 1 + 0 = 1

入力: n = 5674
出力: 4
説明:
ステップ 1: 5 + 6 + 7 + 4 = 22
ステップ 2: 2 + 2 = 4

目次

[単純なアプローチ] 数字を繰り返し加算することによる

このアプローチは、デジタル roo の計算に焦点を当てています。 t 1 桁の値が得られるまで繰り返し数字を合計した結果である数値。概念的な仕組みは次のとおりです。

  1. 数字を合計します : 指定された数値のすべての桁を加算することから始めます。
  2. 結果を確認する : 合計が 1 桁の数値 (つまり 10 未満) の場合は、停止して返します。
  3. プロセスを繰り返す : 合計がまだ 1 桁を超えている場合は、桁の合計でプロセスを繰り返します。これは、合計が 1 桁に達するまで続きます。
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  ));   

出力
1 

時間計算量: O(ログ 10 n) 数値の桁を反復しているとき。
補助スペース: ○(1)

【想定されるアプローチ】数式を利用する

10 進法のすべての数値は、その桁の合計に 10 の累乗を乗算して表現できることがわかっています。たとえば、次のように表される数値です。 ABCD 次のように書くことができます:

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

数字を区切って、これを次のように書き直すことができます。
abcd = a + b + c + d + (a*999 + b*99 + c*9)
abcd = a + b + c + d + 9*(a*111 + b*11 + c)

これは、任意の数値はその桁の合計に 9 の倍数を加えたものとして表現できることを意味します。
したがって、各辺が 9 であるモジュロを取ると、
abcd % 9 = (a + b + c + d) % 9 + 0

これは、abcd を 9 で割った余りが、その桁の合計 (a + b + c + d) を 9 で割った余りと等しいことを意味します。

桁の合計自体が複数の桁で構成されている場合は、この合計をその桁の合計に 9 の倍数を加えたものとしてさらに表すことができます。したがって、9 を法として計算すると、桁の合計が 1 桁の数になるまで 9 の倍数が消去されます。

その結果、任意の数値の桁の合計は、そのモジュロ 9 と等しくなります。モジュロ演算の結果が 0 の場合、1 桁の結果が 9 であることを示します。
コードの実装について知りたい場合は、以下を参照してください。 指定された大きな整数のデジタル ルート (デジタル和の繰り返し)