Dados dos números positivos N y X . La tarea es encontrar la suma de dígitos de un número formado por N repitiendo X número de veces hasta que la suma se convierta en un solo dígito.
Ejemplos:
Input : N = 24, X = 3 Output : 9 Number formed after repeating 24 three time = 242424 Sum = 2 + 4 + 2 + 4 + 2 + 4 = 18 Sum is not the single digit, so finding the sum of digits of 18, 1 + 8 = 9 Input : N = 4, X = 4 Output : 7
Como se discutió en esta publicación, la suma recursiva de dígitos es 9 si el número es múltiplo de 9, de lo contrario, n % 9. Dado que la divisibilidad y la aritmética modular son compatibles con la multiplicación, simplemente encontramos el resultado para una sola ocurrencia, multiplicamos el resultado con x y nuevamente encontramos el resultado.
¿Como funciona esto?
Sean N = 24 y X = 3.
Entonces, sumUntilSingle(N) = 2 + 4 = 6.
Multiplicar 6 por 3 = 18
sumUntilSingle(18) = 9.
A continuación se muestra la implementación de este enfoque:
C++
// C++ program to find Sum of digits of a // number formed by repeating a number X number of // times until sum become single digit. #include <bits/stdc++.h> using namespace std; // return single digit sum of a number. int digSum(int n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } // Returns recursive sum of digits of a number // formed by repeating a number X number of // times until sum become single digit. int repeatedNumberSum(int n, int x) { int sum = x*digSum(n); return digSum(sum); } // Driver program int main() { int n = 24, x = 3; cout << repeatedNumberSum(n, x) << endl; return 0; }
Java
// Java program to find Sum of digits of a // number formed by repeating a number X number of // times until sum become single digit. class GFG { // return single digit sum of a number. static int digSum(int n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } // Returns recursive sum of digits of a number // formed by repeating a number X number of // times until sum become single digit. static int repeatedNumberSum(int n, int x) { int sum = x * digSum(n); return digSum(sum); } // Driver program public static void main (String[] args) { int n = 24, x = 3; System.out.println(repeatedNumberSum(n, x)); } } // This code is contributed by Ajit.
Python3
# Python program to find Sum of digits of a # number formed by repeating a number X number # of times until sum become single digit. # Return single digit sum of a number def digSum(n): if n == 0: return 0 return (n % 9 == 0) and 9 or (n % 9) # Returns recursive sum of digits of a number # formed by repeating a number X number of # times until sum become single digit. def repeatedNumberSum(n, x): sum = x * digSum(n) return digSum(sum) # Driver Code n = 24; x = 3 print(repeatedNumberSum(n, x)) # This code is contributed by Ajit.
C#
// C# program to find Sum of digits of a // number formed by repeating a number X // number of times until sum becomes // single digit. using System; public class GFG { // return single digit sum of a number. static int digSum(int n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } // Returns recursive sum of digits of a // number formed by repeating a number X // number of times until sum become // single digit. static int repeatedNumberSum(int n, int x) { int sum = x * digSum(n); return digSum(sum); } // driver program public static void Main () { int n = 24, x = 3; Console.Write( repeatedNumberSum(n, x)); } } // This code is contributed by Sam007
PHP
<?php // PHP program to find Sum // of digits of a number // formed by repeating a number // X number of times until // sum becomes single digit. // return single digit // sum of a number. function digSum($n) { if ($n == 0) return 0; return ($n % 9 == 0) ? 9 : ($n % 9); } // Returns recursive sum of // digits of a number formed // by repeating a number X // number of times until sum // become single digit. function repeatedNumberSum( $n, $x) { $sum = $x * digSum($n); return digSum($sum); } // Driver Code $n = 24; $x = 3; echo repeatedNumberSum($n, $x); // This code is contributed by anuj_67. ?>
Javascript
<script> // Javascript program to find Sum // of digits of a number formed by // repeating a number X number of // times until sum becomes single digit. // Return single digit // sum of a number. function digSum(n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } // Returns recursive sum of // digits of a number formed // by repeating a number X // number of times until sum // become single digit. function repeatedNumberSum(n, x) { sum = x * digSum(n); return digSum(sum); } // Driver Code let n = 24; let x = 3; document.write(repeatedNumberSum(n, x)); // This code is contributed by _saurabh_jaiswal. </script>
Producción :
9