Máximo de suma y producto de dígitos hasta que el número se reduce a un solo dígito

Dado un número N, la tarea es imprimir el máximo entre la suma y la multiplicación de los dígitos del número dado hasta que el número se reduzca a un solo dígito. 
Nota: La suma y la multiplicación de dígitos se realizarán hasta que el número se reduzca a un solo dígito. 
 

Tomemos un ejemplo donde N = 19,

19 se divide en 1+9=10 luego 10 se divide en 1+0=1. 1 es una suma de un solo dígito. 
Además, 19 se divide en 1*9 = 9. 9 es una multiplicación de un solo dígito. 
Entonces, la salida es 9, es decir, un máximo de 9 y 1. 
 

Input: N = 631
Output: 8

Input: 110
Output: 2

Acercarse:  

  1. Comprueba si un número es menor que 10, entonces la suma y el producto serán iguales. Entonces, devuelve ese número.
  2. Más,
  3. Devuelve el máximo de ambos.

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

C++

// CPP implementation of above approach
#include<bits/stdc++.h>
using namespace std;
    // Function to sum the digits until it
    // becomes a single digit
    long repeatedSum(long n)
    {
        if (n == 0)
            return 0;
        return (n % 9 == 0) ? 9 : (n % 9);
    }
 
    // Function to product the digits until it
    // becomes a single digit
    long repeatedProduct(long n)
    {
        long prod = 1;
 
        // Loop to do sum while
        // sum is not less than
        // or equal to 9
        while (n > 0 || prod > 9) {
            if (n == 0) {
                n = prod;
                prod = 1;
            }
            prod *= n % 10;
            n /= 10;
        }
        return prod;
    }
 
    // Function to find the maximum among
    // repeated sum and repeated product
    long maxSumProduct(long N)
    {
 
        if (N < 10)
            return N;
 
        return max(repeatedSum(N), repeatedProduct(N));
    }
 
    // Driver code
    int main()
    {
 
        long n = 631;
        cout << maxSumProduct(n)<<endl;
        return 0;
    }
// This code is contributed by mits

Java

// Java implementation of above approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
 
    // Function to sum the digits until it
    // becomes a single digit
    public static long repeatedSum(long n)
    {
        if (n == 0)
            return 0;
        return (n % 9 == 0) ? 9 : (n % 9);
    }
 
    // Function to product the digits until it
    // becomes a single digit
    public static long repeatedProduct(long n)
    {
        long prod = 1;
 
        // Loop to do sum while
        // sum is not less than
        // or equal to 9
        while (n > 0 || prod > 9) {
            if (n == 0) {
                n = prod;
                prod = 1;
            }
            prod *= n % 10;
            n /= 10;
        }
        return prod;
    }
 
    // Function to find the maximum among
    // repeated sum and repeated product
    public static long maxSumProduct(long N)
    {
 
        if (N < 10)
            return N;
 
        return Math.max(repeatedSum(N), repeatedProduct(N));
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        long n = 631;
        System.out.println(maxSumProduct(n));
    }
}

Python3

# Python 3 implementation of above approach
 
# Function to sum the digits until
# it becomes a single digit
def repeatedSum(n):
    if (n == 0):
        return 0
    return 9 if(n % 9 == 0) else (n % 9)
 
# Function to product the digits
# until it becomes a single digit
def repeatedProduct(n):
    prod = 1
 
    # Loop to do sum while
    # sum is not less than
    # or equal to 9
    while (n > 0 or prod > 9) :
        if (n == 0) :
            n = prod
            prod = 1
             
        prod *= n % 10
        n //= 10
     
    return prod
 
# Function to find the maximum among
# repeated sum and repeated product
def maxSumProduct(N):
 
    if (N < 10):
        return N
 
    return max(repeatedSum(N),
               repeatedProduct(N))
 
# Driver code
if __name__ == "__main__":
 
    n = 631
    print(maxSumProduct(n))
 
# This code is contributed
# by ChitraNayal

C#

// C# implementation of
// above approach
using System;
class GFG
{
 
// Function to sum the digits
// until it becomes a single digit
public static long repeatedSum(long n)
{
    if (n == 0)
        return 0;
    return (n % 9 == 0) ?
                      9 : (n % 9);
}
 
// Function to product the digits
// until it becomes a single digit
public static long repeatedProduct(long n)
{
    long prod = 1;
 
    // Loop to do sum while
    // sum is not less than
    // or equal to 9
    while (n > 0 || prod > 9)
    {
        if (n == 0)
        {
            n = prod;
            prod = 1;
        }
        prod *= n % 10;
        n /= 10;
    }
    return prod;
}
 
// Function to find the maximum among
// repeated sum and repeated product
public static long maxSumProduct(long N)
{
 
    if (N < 10)
        return N;
 
    return Math.Max(repeatedSum(N),
                    repeatedProduct(N));
}
 
// Driver code
public static void Main()
{
    long n = 631;
    Console.WriteLine(maxSumProduct(n));
}
}
 
// This code is contributed
// by inder_verma

Javascript

<script>
// javascript implementation of above approach
    // Function to sum the digits until it
    // becomes a single digit
    function repeatedSum(n) {
        if (n == 0)
            return 0;
        return (n % 9 == 0) ? 9 : (n % 9);
    }
 
    // Function to product the digits until it
    // becomes a single digit
    function repeatedProduct(n) {
        var prod = 1;
 
        // Loop to do sum while
        // sum is not less than
        // or equal to 9
        while (n > 0 || prod > 9) {
            if (n == 0) {
                n = prod;
                prod = 1;
            }
            prod *= n % 10;
            n = parseInt(n/10);
        }
        return prod;
    }
 
    // Function to find the maximum among
    // repeated sum and repeated product
    function maxSumProduct(N) {
 
        if (N < 10)
            return N;
 
        return Math.max(repeatedSum(N), repeatedProduct(N));
    }
 
    // Driver code
     
 
        var n = 631;
        document.write(maxSumProduct(n));
 
// This code contributed by aashish1995
</script>
Producción: 

8

 

Publicación traducida automáticamente

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