Count of numbers whose sum of increasing powers of digits is equal to the number itself

Given an integer N, the task is to count all the integers less than or equal to N that follow the property where the sum of their digits raised to the power (starting from 1 and increased by 1 each time) is equal to the integer itself i.e. if D1D2D3…DN is an N digit number then for it to satisfy the given property (D11 + D22 + D33 + … + DNN) must be equal to D1D2D3…DN.
Examples:Â
Â
Input: N = 100Â
Output: 11Â
01 = 0Â
11 = 1Â
21 = 2Â
…Â
91 = 9Â
81 + 92 = 8 + 81 = 89
Input: N = 200Â
Output: 13Â
Â
Â
Approach: Initialise count = 0 and for every number from 0 to N, find the sum of digits raised to the increasing power and if the resultant sum is equal to the number itself then increment the count. Finally, print the count.
Below is the implementation of the above approach:Â
Â
CPP
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the// count of digits of nint countDigits(int n){Â Â Â Â int cnt = 0;Â Â Â Â while (n > 0) {Â Â Â Â Â Â Â Â cnt++;Â Â Â Â Â Â Â Â n /= 10;Â Â Â Â }Â Â Â Â return cnt;}Â
// Function to return the sum of// increasing powers of Nint 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 conditionsint 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 codeint 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 ndef countDigits(n):Â Â Â Â cnt = 0Â Â Â Â while (n > 0):Â Â Â Â Â Â Â Â cnt += 1Â Â Â Â Â Â Â Â n //= 10Â Â Â Â return cntÂ
# Function to return the sum of# increasing powers of Ndef 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 conditionsdef 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 coden = 200Â
print(countNum(n))Â
# This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approachusing 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 nfunction 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 Nfunction 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 conditionsfunction 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> |
13
Â
Time Complexity: O(n * 2log10n)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



