Dado un número entero N , la tarea es imprimir los primeros N términos cuya suma de dígitos sea un múltiplo de 10 . Los primeros términos de la serie son 19, 28, 37, 46, 55, …
Ejemplos:
Entrada: N = 5
Salida: 19 28 37 46 55
Entrada: N = 10
Salida: 19 28 37 46 55 64 73 82 91 109
Planteamiento: Se puede observar que para obtener el N- ésimo término de la serie requerida, encuentre la suma de los dígitos de N. Si la suma ya es un múltiplo de 10 , agregue el dígito 0 al final de N ; de lo contrario, agregue el dígito mínimo posible al final, de modo que la nueva suma de dígitos sea un múltiplo de 10 .
Por ejemplo, para obtener el término 19 , dado que la suma de los dígitos ya es un múltiplo de 10 , agregue 0 y 190 es el término 19 de la serie.
Para N = 5 , el dígito mínimo que se puede agregar para hacer la suma de dígitos como un múltiplo de 10 es 5 y 55 es el quinto término de la serie.
A continuación se muestra la implementación del enfoque anterior:
C++
#include <bits/stdc++.h> using namespace std; const int TEN = 10; // Function to return the // sum of digits of n int digitSum(int n) { int sum = 0; while (n > 0) { // Add last digit to the sum sum += n % TEN; // Remove last digit n /= TEN; } return sum; } // Function to return the nth term // of the required series int getNthTerm(int n) { int sum = digitSum(n); // If sum of digit is already // a multiple of 10 then append 0 if (sum % TEN == 0) return (n * TEN); // To store the minimum digit // that must be appended int extra = TEN - (sum % TEN); // Return n after appending // the required digit return ((n * TEN) + extra); } // Function to print the first n terms // of the required series void firstNTerms(int n) { for (int i = 1; i <= n; i++) cout << getNthTerm(i) << " "; } // Driver code int main() { int n = 10; firstNTerms(n); return 0; }
Java
// Java implementation of the above approach class GFG { final static int TEN = 10; // Function to return the // sum of digits of n static int digitSum(int n) { int sum = 0; while (n > 0) { // Add last digit to the sum sum += n % TEN; // Remove last digit n /= TEN; } return sum; } // Function to return the nth term // of the required series static int getNthTerm(int n) { int sum = digitSum(n); // If sum of digit is already // a multiple of 10 then append 0 if (sum % TEN == 0) return (n * TEN); // To store the minimum digit // that must be appended int extra = TEN - (sum % TEN); // Return n after appending // the required digit return ((n * TEN) + extra); } // Function to print the first n terms // of the required series static void firstNTerms(int n) { for (int i = 1; i <= n; i++) System.out.print(getNthTerm(i) + " "); } // Driver code public static void main (String[] args) { int n = 10; firstNTerms(n); } } // This code is contributed by AnkitRai01
Python3
# Python3 code for above implementation TEN = 10 # Function to return the # sum of digits of n def digitSum(n): sum = 0 while (n > 0): # Add last digit to the sum sum += n % TEN # Remove last digit n //= TEN return sum # Function to return the nth term # of the required series def getNthTerm(n): sum = digitSum(n) # If sum of digit is already # a multiple of 10 then append 0 if (sum % TEN == 0): return (n * TEN) # To store the minimum digit # that must be appended extra = TEN - (sum % TEN) # Return n after appending # the required digit return ((n * TEN) + extra) # Function to print the first n terms # of the required series def firstNTerms(n): for i in range(1, n + 1): print(getNthTerm(i), end = " ") # Driver code n = 10 firstNTerms(n) # This code is contributed by Mohit Kumar
C#
// C# Program to Find the Unique elements // in linked lists using System; class GFG { readonly static int TEN = 10; // Function to return the // sum of digits of n static int digitSum(int n) { int sum = 0; while (n > 0) { // Add last digit to the sum sum += n % TEN; // Remove last digit n /= TEN; } return sum; } // Function to return the nth term // of the required series static int getNthTerm(int n) { int sum = digitSum(n); // If sum of digit is already // a multiple of 10 then append 0 if (sum % TEN == 0) return (n * TEN); // To store the minimum digit // that must be appended int extra = TEN - (sum % TEN); // Return n after appending // the required digit return ((n * TEN) + extra); } // Function to print the first n terms // of the required series static void firstNTerms(int n) { for (int i = 1; i <= n; i++) Console.Write(getNthTerm(i) + " "); } // Driver code public static void Main (String[] args) { int n = 10; firstNTerms(n); } } // This code is contributed by 29AjayKumar
Javascript
<script> const TEN = 10; // Function to return the // sum of digits of n function digitSum(n) { let sum = 0; while (n > 0) { // Add last digit to the sum sum += n % TEN; // Remove last digit n = Math.floor(n / TEN); } return sum; } // Function to return the nth term // of the required series function getNthTerm(n) { let sum = digitSum(n); // If sum of digit is already // a multiple of 10 then append 0 if (sum % TEN == 0) return (n * TEN); // To store the minimum digit // that must be appended let extra = TEN - (sum % TEN); // Return n after appending // the required digit return ((n * TEN) + extra); } // Function to print the first n terms // of the required series function firstNTerms(n) { for (let i = 1; i <= n; i++) document.write(getNthTerm(i) + " "); } // Driver code let n = 10; firstNTerms(n); // This code is contributed by Surbhi Tyagi. </script>
19 28 37 46 55 64 73 82 91 109