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
#include <bits/stdc++.h>
using namespace std;
int countDigits( int n)
{
int cnt = 0;
while (n > 0) {
cnt++;
n /= 10;
}
return cnt;
}
int digitPowSum( int n)
{
int sum = 0;
int pw = countDigits(n);
while (n > 0) {
int d = n % 10;
sum += pow (d, pw);
pw--;
n /= 10;
}
return sum;
}
int countNum( int n)
{
int count = 0;
for ( int i = 0; i <= n; i++) {
if (i == digitPowSum(i)) {
count++;
}
}
return count;
}
int main()
{
int n = 200;
cout << countNum(n);
return 0;
}
|
Java
class GFG
{
static int countDigits( int n)
{
int cnt = 0 ;
while (n > 0 )
{
cnt++;
n /= 10 ;
}
return cnt;
}
static int digitPowSum( int n)
{
int sum = 0 ;
int pw = countDigits(n);
while (n > 0 )
{
int d = n % 10 ;
sum += Math.pow(d, pw);
pw--;
n /= 10 ;
}
return sum;
}
static int countNum( int n)
{
int count = 0 ;
for ( int i = 0 ; i <= n; i++)
{
if (i == digitPowSum(i))
{
count++;
}
}
return count;
}
public static void main (String[] args)
{
int n = 200 ;
System.out.println(countNum(n));
}
}
|
Python
def countDigits(n):
cnt = 0
while (n > 0 ):
cnt + = 1
n / / = 10
return cnt
def digitPowSum(n):
sum = 0
pw = countDigits(n)
while (n > 0 ):
d = n % 10
sum + = pow (d, pw)
pw - = 1
n / / = 10
return sum
def countNum(n):
count = 0
for i in range (n + 1 ):
if (i = = digitPowSum(i)):
count + = 1
return count
n = 200
print (countNum(n))
|
C#
using System;
class GFG
{
static int countDigits( int n)
{
int cnt = 0;
while (n > 0)
{
cnt++;
n /= 10;
}
return cnt;
}
static int digitPowSum( int n)
{
int sum = 0;
int pw = countDigits(n);
while (n > 0)
{
int d = n % 10;
sum += ( int ) Math.Pow(d, pw);
pw--;
n /= 10;
}
return sum;
}
static int countNum( int n)
{
int count = 0;
for ( int i = 0; i <= n; i++)
{
if (i == digitPowSum(i))
count++;
}
return count;
}
public static void Main (String[] args)
{
int n = 200;
Console.WriteLine(countNum(n));
}
}
|
Javascript
<script>
function countDigits(n)
{
let cnt = 0;
while (n > 0) {
cnt++;
n = Math.floor(n / 10);
}
return cnt;
}
function digitPowSum(n)
{
let sum = 0;
let pw = countDigits(n);
while (n > 0) {
let d = n % 10;
sum += Math.pow(d, pw);
pw--;
n = Math.floor(n / 10);
}
return sum;
}
function countNum(n)
{
let count = 0;
for (let i = 0; i <= n; i++) {
if (i == digitPowSum(i)) {
count++;
}
}
return count;
}
let n = 200;
document.write(countNum(n));
</script>
|
Time Complexity: O(n * 2log10n)
Auxiliary Space: O(1)