Conteo de números cuya suma de potencias crecientes de dígitos es igual al número mismo

Dado un entero N , la tarea es contar todos los enteros menores o iguales a N que siguen la propiedad donde la suma de sus dígitos elevados a la potencia (comenzando desde 1 y aumentado en 1 cada vez) es igual al entero mismo es decir, si D 1 D 2 D 3 …D N es un número de N dígitos, entonces para que satisfaga la propiedad dada (D 1 1 + D 2 2 + D 3 3 + … + D N N ) debe ser igual a D 1 D 2 re 3 …ren _
Ejemplos: 
 

Entrada: N = 100 
Salida: 11 
0 1 = 0 
1 1 = 1 
2 1 = 2 
… 
9 1 = 9 
8 1 + 9 2 = 8 + 81 = 89
Entrada: N = 200 
Salida: 13 
 

Enfoque: Inicialice el conteo = 0 y para cada número de 0 a N , encuentre la suma de los dígitos elevados a la potencia creciente y, si la suma resultante es igual al número en sí, incremente el conteo . Finalmente, imprima el conteo .
A continuación se muestra la implementación del enfoque anterior: 
 

CPP

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the
// count of digits of n
int countDigits(int n)
{
    int cnt = 0;
    while (n > 0) {
        cnt++;
        n /= 10;
    }
    return cnt;
}
 
// Function to return the sum of
// increasing powers of N
int digitPowSum(int n)
{
 
    // To store the required answer
    int sum = 0;
 
    // Count of digits in n which will
    // be the power of the last digit
    int pw = countDigits(n);
 
    // While there are digits left
    while (n > 0) {
 
        // Get the last digit
        int d = n % 10;
 
        // Add the last digit after raising
        // it to the required power
        sum += pow(d, pw);
 
        // Decrement the power for
        // the previous digit
        pw--;
 
        // Remove the last digit
        n /= 10;
    }
    return sum;
}
 
// Function to return the count
// of integers which satisfy
// the given conditions
int countNum(int n)
{
    int count = 0;
 
    for (int i = 0; i <= n; i++) {
 
        // If current element satisfies
        // the given condition
        if (i == digitPowSum(i)) {
            count++;
        }
    }
    return count;
}
 
// Driver code
int main()
{
    int n = 200;
 
    cout << countNum(n);
 
    return 0;
}

Java

// Java implementation of the approach
 
class GFG
{
     
    // Function to return the
    // count of digits of n
    static int countDigits(int n)
    {
        int cnt = 0;
        while (n > 0)
        {
            cnt++;
            n /= 10;
        }
        return cnt;
    }
     
    // Function to return the sum of
    // increasing powers of N
    static int digitPowSum(int n)
    {
     
        // To store the required answer
        int sum = 0;
     
        // Count of digits in n which will
        // be the power of the last digit
        int pw = countDigits(n);
     
        // While there are digits left
        while (n > 0)
        {
     
            // Get the last digit
            int d = n % 10;
     
            // Add the last digit after raising
            // it to the required power
            sum += Math.pow(d, pw);
     
            // Decrement the power for
            // the previous digit
            pw--;
     
            // Remove the last digit
            n /= 10;
        }
        return sum;
    }
     
    // Function to return the count
    // of integers which satisfy
    // the given conditions
    static int countNum(int n)
    {
        int count = 0;
     
        for (int i = 0; i <= n; i++)
        {
     
            // If current element satisfies
            // the given condition
            if (i == digitPowSum(i))
            {
                count++;
            }
        }
        return count;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 200;
     
        System.out.println(countNum(n));
     
    }
}
 
// This code is contributed by AnkitRai01

Python

# Python3 implementation of the approach
 
# Function to return the
# count of digits of n
def countDigits(n):
    cnt = 0
    while (n > 0):
        cnt += 1
        n //= 10
    return cnt
 
# Function to return the sum of
# increasing powers of N
def digitPowSum(n):
 
    # To store the required answer
    sum = 0
 
    # Count of digits in n which will
    # be the power of the last digit
    pw = countDigits(n)
 
    # While there are digits left
    while (n > 0):
 
        # Get the last digit
        d = n % 10
 
        # Add the last digit after raising
        # it to the required power
        sum += pow(d, pw)
 
        # Decrement the power for
        # the previous digit
        pw -= 1
 
        # Remove the last digit
        n //= 10
    return sum
 
# Function to return the count
# of integers which satisfy
# the given conditions
def countNum(n):
 
    count = 0
 
    for i in range(n + 1):
 
        # If current element satisfies
        # the given condition
        if (i == digitPowSum(i)):
            count += 1
 
    return count
 
# Driver code
n = 200
 
print(countNum(n))
 
# This code is contributed by mohit kumar 29

C#

// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the
    // count of digits of n
    static int countDigits(int n)
    {
        int cnt = 0;
        while (n > 0)
        {
            cnt++;
            n /= 10;
        }
        return cnt;
    }
     
    // Function to return the sum of
    // increasing powers of N
    static int digitPowSum(int n)
    {
     
        // To store the required answer
        int sum = 0;
     
        // Count of digits in n which will
        // be the power of the last digit
        int pw = countDigits(n);
     
        // While there are digits left
        while (n > 0)
        {
     
            // Get the last digit
            int d = n % 10;
     
            // Add the last digit after raising
            // it to the required power
            sum += (int) Math.Pow(d, pw);
     
            // Decrement the power for
            // the previous digit
            pw--;
     
            // Remove the last digit
            n /= 10;
        }
        return sum;
    }
     
    // Function to return the count
    // of integers which satisfy
    // the given conditions
    static int countNum(int n)
    {
        int count = 0;
     
        for (int i = 0; i <= n; i++)
        {
     
            // If current element satisfies
            // the given condition
            if (i == digitPowSum(i))
                count++;
        }
        return count;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int n = 200;
     
        Console.WriteLine(countNum(n));
     
    }
}
 
// This code is contributed by
// sanjeev2552

Javascript

<script>
 
// JavaScript implementation of the approach
 
// Function to return the
// count of digits of n
function countDigits(n)
{
    let cnt = 0;
    while (n > 0) {
        cnt++;
        n = Math.floor(n / 10);
    }
    return cnt;
}
 
// Function to return the sum of
// increasing powers of N
function digitPowSum(n)
{
 
    // To store the required answer
    let sum = 0;
 
    // Count of digits in n which will
    // be the power of the last digit
    let pw = countDigits(n);
 
    // While there are digits left
    while (n > 0) {
 
        // Get the last digit
        let d = n % 10;
 
        // Add the last digit after raising
        // it to the required power
        sum += Math.pow(d, pw);
 
        // Decrement the power for
        // the previous digit
        pw--;
 
        // Remove the last digit
        n = Math.floor(n / 10);
    }
    return sum;
}
 
// Function to return the count
// of integers which satisfy
// the given conditions
function countNum(n)
{
    let count = 0;
 
    for (let i = 0; i <= n; i++) {
 
        // If current element satisfies
        // the given condition
        if (i == digitPowSum(i)) {
            count++;
        }
    }
    return count;
}
 
// Driver code
 
    let n = 200;
 
    document.write(countNum(n));
 
// This code is contributed by Surbhi Tyagi.
 
</script>
Producción: 

13

 

Complejidad del tiempo: O(n * 2log 10 n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

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