Number is equal to nth power of summation of its digits

1.2k Views Asked by At

I am working on problem from code wars. Following is the question

The number $81$ has a special property, a certain power of the sum of its digits is equal to $81$ (nine squared). Eighty one ($81$), is the first number in having this property (not considering numbers of one digit). The next one, is $512$. Let's see both cases with the details

$8 + 1 = 9$ and $9^2 = 81$

$512 = 5 + 1 + 2 = 8$ and $8^3 = 512$

We need to make a function, power_sumDigTerm(), that receives a number n and may output the n-th term of this sequence of numbers. The cases we presented above means that

power_sumDigTerm($1$) == $81$

power_sumDigTerm($2$) == $512$

Happy coding!

Only possible way I can think to solve this problem is by using brute force approach. But it is too time consuming Is there any mathematical theory I can apply to solve this problem.

1

There are 1 best solutions below

3
On

Denis,

try to calculate all powers (2 to 180) ^ (2 to 63), sort this and remove duplicates (3^4 == 9^2).

Now you can go in the list of powers and check if powering the sum of the digits will be the original number. I.e: use your brute force only for this items.

I finished the challenge with total calculation time of 52ms from server. But to achieve this I reduced the range to fit the problem.