El menor número mayor o igual a X cuya suma de dígitos es divisible por Y

Dados dos enteros X e Y , la tarea es encontrar el número más pequeño mayor o igual a X cuya suma de dígitos sea divisible por  Y.
Nota: 1 <= X <= 1000 , 1 <= Y <= 50 .
Ejemplos: 
 

Entrada: X = 10, Y = 5 
Salida: 14 
Explicación: 
14 es el número más pequeño mayor que 10 cuya suma de dígitos (1+4 = 5) es divisible por 5. 
Entrada: X = 5923, Y = 13 
Salida: 5939 
 

Enfoque: la idea de este problema es ejecutar un bucle desde X y verificar para cada entero si su suma de dígitos es divisible por Y o no. Devuelve el primer número cuya suma de dígitos es divisible por Y . Dadas las restricciones de X e Y , la respuesta siempre existe. 
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to find the smallest number greater than or
// equal to X and divisible by Y
 
#include <bits/stdc++.h>
using namespace std;
 
#define MAXN 10000000
 
// Function that returns the sum of digits of a number
int sumOfDigits(int n)
{
    // Initialize variable to store the sum
    int sum = 0;
    while (n > 0) {
        // Add the last digit of the number
        sum += n % 10;
        // Remove the last digit from the number
        n /= 10;
    }
    return sum;
}
 
// Function that returns the smallest number greater than or
// equal to X and divisible by Y
int smallestNum(int X, int Y)
{
    // Initialize result variable
    int res = -1;
    // Loop through numbers greater than  equal to X
    for (int i = X; i < MAXN; i++) {
        // Calculate sum of digits
        int sum_of_digit = sumOfDigits(i);
        // Check if sum of digits is divisible by Y
        if (sum_of_digit % Y == 0) {
            res = i;
            break;
        }
    }
    return res;
}
 
// Driver code
int main()
{
    int X = 5923, Y = 13;
    cout << smallestNum(X, Y);
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta

C

// C program to find the smallest number greater than or
// equal to X and divisible by Y
#include <stdio.h>
 
#define MAXN 10000000
 
// Function that returns the sum of digits of a number
int sumOfDigits(int n)
{
    // Initialize variable to store the sum
    int sum = 0;
    while (n > 0) {
        // Add the last digit of the number
        sum += n % 10;
        // Remove the last digit from the number
        n /= 10;
    }
    return sum;
}
 
// Function that returns the smallest number greater than or
// equal to X and divisible by Y
int smallestNum(int X, int Y)
{
    // Initialize result variable
    int res = -1;
    // Loop through numbers greater than  equal to X
    for (int i = X; i < MAXN; i++) {
        // Calculate sum of digits
        int sum_of_digit = sumOfDigits(i);
        // Check if sum of digits is divisible by Y
        if (sum_of_digit % Y == 0) {
            res = i;
            break;
        }
    }
    return res;
}
 
// Driver code
int main()
{
    int X = 5923, Y = 13;
    printf("%d", smallestNum(X, Y));
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta

Java

// Java program to find the smallest number
// greater than or equal to X and divisible by Y
 
class GFG{
 
static final int MAXN = 10000000;
 
// Function that returns the sum
// of digits of a number
static int sumOfDigits(int n)
{
     
    // Initialize variable to
    // store the sum
    int sum = 0;
    while (n > 0)
    {
 
         // Add the last digit
         // of the number
         sum += n % 10;
 
         // Remove the last digit
         // from the number
         n /= 10;
    }
    return sum;
}
 
// Function that returns the smallest number
// greater than or equal to X and divisible by Y
static int smallestNum(int X, int Y)
{
     
    // Initialize result variable
    int res = -1;
 
    // Loop through numbers greater
    // than equal to X
    for (int i = X; i < MAXN; i++)
    {
 
        // Calculate sum of digits
        int sum_of_digit = sumOfDigits(i);
 
        // Check if sum of digits
        // is divisible by Y
        if (sum_of_digit % Y == 0)
        {
            res = i;
            break;
        }
    }
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    int X = 5923, Y = 13;
    System.out.print(smallestNum(X, Y));
}
}
 
// This code is contributed by Rohit_ranjan

Python3

# Python3 program to find the smallest number
# greater than or equal to X and divisible by Y
 
MAXN = 10000000
 
# Function that returns the 
# sum of digits of a number
def sumOfDigits(n):
     
    # Initialize variable 
    # to store the sum
    sum = 0
     
    while(n > 0):
         
        # Add the last digit
        # of the number
        sum += n % 10
         
        # Remove the last digit
        # from the number
        n //= 10
         
    return sum
 
# Function that returns the smallest number
# greater than or equal to X and divisible by Y
def smallestNum(X, Y):
     
    # Initialize result variable
    res = -1;
 
    # Loop through numbers greater
    # than equal to X
    for i in range(X, MAXN):
         
        # Calculate sum of digits
        sum_of_digit = sumOfDigits(i)
         
        # Check if sum of digits
        # is divisible by Y
        if sum_of_digit % Y == 0:
            res = i
            break
     
    return res    
             
# Driver code
if __name__=='__main__':
     
    (X, Y) = (5923, 13)
      
    print(smallestNum(X, Y))
 
# This code is contributed by rutvik_56

C#

// C# program to find the smallest number
// greater than or equal to X and divisible by Y
using System;
 
class GFG{
 
static readonly int MAXN = 10000000;
 
// Function that returns the sum
// of digits of a number
static int sumOfDigits(int n)
{
     
    // Initialize variable to
    // store the sum
    int sum = 0;
    while(n > 0)
    {
         
         // Add the last digit
         // of the number
         sum += n % 10;
 
         // Remove the last digit
         // from the number
         n /= 10;
    }
    return sum;
}
 
// Function that returns the smallest number
// greater than or equal to X and divisible by Y
static int smallestNum(int X, int Y)
{
     
    // Initialize result variable
    int res = -1;
 
    // Loop through numbers greater
    // than equal to X
    for(int i = X; i < MAXN; i++)
    {
     
        // Calculate sum of digits
        int sum_of_digit = sumOfDigits(i);
 
        // Check if sum of digits
        // is divisible by Y
        if(sum_of_digit % Y == 0)
        {
           res = i;
           break;
        }
    }
    return res;
}
 
// Driver code
public static void Main(String[] args)
{
    int X = 5923, Y = 13;
    Console.Write(smallestNum(X, Y));
}
}
 
// This code is contributed by gauravrajput1

Javascript

<script>
 
// javascript program to find the smallest number
// greater than or equal to X and divisible by Y
var MAXN = 10000000;
 
// Function that returns the sum
// of digits of a number
function sumOfDigits(n)
{
     
    // Initialize variable to
    // store the sum
    var sum = 0;
    while (n > 0)
    {
 
         // Add the last digit
         // of the number
         sum += n % 10;
 
         // Remove the last digit
         // from the number
         n  = parseInt(n/10);
    }
    return sum;
}
 
// Function that returns the smallest number
// greater than or equal to X and divisible by Y
function smallestNum(X , Y)
{
     
    // Initialize result variable
    var res = -1;
 
    // Loop through numbers greater
    // than equal to X
    for (i = X; i < MAXN; i++)
    {
 
        // Calculate sum of digits
        var sum_of_digit = sumOfDigits(i);
 
        // Check if sum of digits
        // is divisible by Y
        if (sum_of_digit % Y == 0)
        {
            res = i;
            break;
        }
    }
    return res;
}
 
// Driver code
var X = 5923, Y = 13;
document.write(smallestNum(X, Y));
 
 
// This code contributed by shikhasingrajput
</script>
Producción: 

5939

 

Complejidad de tiempo: O (MAXN)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por rupesh_rao 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 *