Encuentra N números tales que un número y su reverso sean divisibles por la suma de sus dígitos

Dado un número N , la tarea es imprimir los primeros N números de modo que cada número y el reverso del número sea divisible por su suma de dígitos .
Ejemplo: 
 

Entrada: N = 4 
Salida: 1 2 3 4 
Explicación: 
El reverso de cada número de un solo dígito es el mismo número. Y, todo número es divisible por sí mismo. 
Entrada: N = 12 
Salida: 1 2 3 4 5 6 7 8 9 10 12 18 
 

Enfoque: La idea es iterar a través de cada número desde el 1 y calcular la suma de los dígitos. Para cada uno de esos números, verifica si el número y el reverso del número son divisibles por la suma o no. Por lo tanto, se siguen los siguientes pasos para calcular la respuesta: 
 

  1. Inicialice el contador a uno e itere todos los números uno por uno.
  2. Para cada número, encuentra el reverso del número .
  3. Mientras encuentra el reverso del número, calcule la suma de los dígitos del número .
  4. Ahora, verifica si el número y el reverso del número son divisibles por la suma de sus dígitos.
  5. Si es así, incremente el contador. Repita los pasos anteriores hasta que este contador sea igual a N .

A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ program to print the first N numbers
// such that every number and the reverse
// of the number is divisible by its
// sum of digits
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the sum of digits
int digit_sum(int n)
{
    int sum = 0, m;
 
    // Loop to iterate through every
    // digit of the number
    while (n > 0) {
        m = n % 10;
        sum = sum + m;
        n = n / 10;
    }
 
    // Returning the sum of digits
    return (sum);
}
 
// Function to calculate the reverse
// of a number
int reverse(int n)
{
    int r = 0;
 
    // Loop to calculate the reverse
    // of the number
    while (n != 0) {
        r = r * 10;
        r = r + n % 10;
        n = n / 10;
    }
 
    // Return the reverse of the
    // number
    return (r);
}
 
// Function to print the first N numbers
// such that every number and the reverse
// of the number is divisible by its
// sum of digits
void operation(int n)
{
    int i = 1, a, count = 0, r;
 
    // Loop to continuously check and
    // generate number until there
    // are n outputs
    while (count < n) {
 
        // Variable to hold the sum of
        // the digit of the number
        a = digit_sum(i);
 
        // Computing the reverse of the
        // number
        r = reverse(i);
 
        // Checking if the condition satisfies.
        // Increment the count and print the
        // number if it satisfies.
        if (i % a == 0 && r % a == 0) {
            cout << i << " ";
            count++;
            i++;
        }
        else
            i++;
    }
}
 
// Driver code
int main()
{
    int n = 10;
 
    operation(n);
}

Java

// Java program to print the first N numbers
// such that every number and the reverse
// of the number is divisible by its
// sum of digits
import java.util.*;
 
class GFG{
 
// Function to calculate the sum of digits
static int digit_sum(int n)
{
    int sum = 0, m;
 
    // Loop to iterate through
    // every digit of the number
    while (n > 0)
    {
        m = n % 10;
        sum = sum + m;
        n = n / 10;
    }
 
    // Returning the sum of digits
    return (sum);
}
 
// Function to calculate the
// reverse of a number
static int reverse(int n)
{
    int r = 0;
 
    // Loop to calculate the
    // reverse of the number
    while (n != 0)
    {
        r = r * 10;
        r = r + n % 10;
        n = n / 10;
    }
 
    // Return the reverse
    // of the number
    return (r);
}
 
// Function to print the first N numbers
// such that every number and the reverse
// of the number is divisible by its
// sum of digits
static void operation(int n)
{
    int i = 1, a, count = 0, r;
 
    // Loop to continuously check and
    // generate number until there
    // are n outputs
    while (count < n)
    {
 
        // Variable to hold the sum
        // of the digit of the number
        a = digit_sum(i);
 
        // Computing the reverse of the
        // number
        r = reverse(i);
 
        // Checking if the condition satisfies.
        // Increment the count and print the
        // number if it satisfies.
        if (i % a == 0 && r % a == 0)
        {
            System.out.print(i + " ");
            count++;
            i++;
        }
        else
            i++;
    }
}
 
// Driver code
public static void main(String args[])
{
    int n = 10;
 
    operation(n);
}
}
 
// This code is contributed by shivanisinghss2110

Python3

# Python3 program to print the first N numbers
# such that every number and the reverse
# of the number is divisible by its
# sum of digits
 
# Function to calculate the sum of digits
def digit_sum(n):
    sum = 0
 
    # Loop to iterate through every
    # digit of the number
    while (n > 0):
        m = n % 10;
        sum = sum + m;
        n = n //10
 
    # Returning the sum of digits
    return (sum)
 
# Function to calculate the reverse
# of a number
def reverse(n):
    r = 0
 
    # Loop to calculate the reverse
    # of the number
    while (n != 0):
        r = r * 10
        r = r + n % 10
        n = n // 10
 
    # Return the reverse of the
    # number
    return (r)
 
# Function to print the first N numbers
# such that every number and the reverse
# of the number is divisible by its
# sum of digits
def operation(n):
    i = 1
    count = 0
 
    # Loop to continuously check and
    # generate number until there
    # are n outputs
    while (count < n):
         
        # Variable to hold the sum of
        # the digit of the number
        a = digit_sum(i)
 
        # Computing the reverse of the
        # number
        r = reverse(i)
 
        # Checking if the condition satisfies.
        # Increment the count and print the
        # number if it satisfies.
        if (i % a == 0 and r % a == 0):
 
            print(i, end = " ")
            count += 1
            i += 1
        else:
            i += 1
 
# Driver code
if __name__ == '__main__':
     
    n = 10
    operation(n)
 
# This code is contributed by Samarth

C#

// C# program to print the first N numbers
// such that every number and the reverse
// of the number is divisible by its
// sum of digits
using System;
class GFG{
 
// Function to calculate the sum of digits
static int digit_sum(int n)
{
    int sum = 0, m;
 
    // Loop to iterate through
    // every digit of the number
    while (n > 0)
    {
        m = n % 10;
        sum = sum + m;
        n = n / 10;
    }
 
    // Returning the sum of digits
    return (sum);
}
 
// Function to calculate the
// reverse of a number
static int reverse(int n)
{
    int r = 0;
 
    // Loop to calculate the
    // reverse of the number
    while (n != 0)
    {
        r = r * 10;
        r = r + n % 10;
        n = n / 10;
    }
 
    // Return the reverse
    // of the number
    return (r);
}
 
// Function to print the first N numbers
// such that every number and the reverse
// of the number is divisible by its
// sum of digits
static void operation(int n)
{
    int i = 1, a, count = 0, r;
 
    // Loop to continuously check and
    // generate number until there
    // are n outputs
    while (count < n)
    {
 
        // Variable to hold the sum
        // of the digit of the number
        a = digit_sum(i);
 
        // Computing the reverse of the
        // number
        r = reverse(i);
 
        // Checking if the condition satisfies.
        // Increment the count and print the
        // number if it satisfies.
        if (i % a == 0 && r % a == 0)
        {
            Console.Write(i + " ");
            count++;
            i++;
        }
        else
            i++;
    }
}
 
// Driver code
public static void Main()
{
    int n = 10;
 
    operation(n);
}
}
 
// This code is contributed by Code_Mech

Javascript

<script>
 
    // Javascript program to print the first N numbers
    // such that every number and the reverse
    // of the number is divisible by its
    // sum of digits
     
    // Function to calculate the sum of digits
    function digit_sum(n)
    {
        let sum = 0, m;
 
        // Loop to iterate through every
        // digit of the number
        while (n > 0) {
            m = n % 10;
            sum = sum + m;
            n = parseInt(n / 10, 10);
        }
 
        // Returning the sum of digits
        return (sum);
    }
 
    // Function to calculate the reverse
    // of a number
    function reverse(n)
    {
        let r = 0;
 
        // Loop to calculate the reverse
        // of the number
        while (n != 0) {
            r = r * 10;
            r = r + n % 10;
            n = parseInt(n / 10, 10);
        }
 
        // Return the reverse of the
        // number
        return (r);
    }
 
    // Function to print the first N numbers
    // such that every number and the reverse
    // of the number is divisible by its
    // sum of digits
    function operation(n)
    {
        let i = 1, a, count = 0, r;
 
        // Loop to continuously check and
        // generate number until there
        // are n outputs
        while (count < n) {
 
            // Variable to hold the sum of
            // the digit of the number
            a = digit_sum(i);
 
            // Computing the reverse of the
            // number
            r = reverse(i);
 
            // Checking if the condition satisfies.
            // Increment the count and print the
            // number if it satisfies.
            if (i % a == 0 && r % a == 0) {
                document.write(i + " ");
                count++;
                i++;
            }
            else
                i++;
        }
    }
     
    let n = 10;
   
    operation(n);
 
</script>
Producción: 

1 2 3 4 5 6 7 8 9 10

 

Complejidad temporal: O(n * log 10 n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por srutichatterjee2024 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *