How would I calculate sum of digits in the number (a^b)?

954 Views Asked by At

I was doing a question from a site,project euler specifically.I came to a question in which I was asked to calculate sum of digits in number 2^1000.Since I program very often I was able to do that question by making array and calculating as We used to do in elementary school.But I was not convinced because How a student calculate that if he/she don't know programming.I mean it is completely biased question isn't it ?

I am asking if there is any way to calculate digit sum in general (a^b)[a to the power b].For student who don't have programming background.

P.S:- If anybody wants to see implementation.I can post here

2

There are 2 best solutions below

0
On

It's a one-liner in Maple:

convert(convert(2^1000, base, 10),`+`);

You could look up OEIS sequence A001370. Or you could just ask Wolfram Alpha.

But if you're asking for a way of doing it by hand, I very much doubt that there is any.

0
On

Well there is a way to find the sum of the digits of $a^b$ when written in base $p$ where $p$ is prime and $p$ is a divisor of $a^b$. Denote by $v_p(n)$ the exponent of $p$ in the prime factorisation of $n$ and $S_p(n)$ the sum of the digits of $n$ when written in base $p$. By Legendre's Formula,

\begin{align*} v_p(a^b!)=\dfrac{a^b-S_p(a^b)}{p-1} &=\sum_{i=1}^{\infty}\lfloor{ \dfrac{a^b}{p^i} \rfloor} \\ \implies{}S_p(a^b)&=a^b-(p-1)\sum_{i=1}^{\infty}\lfloor{ \dfrac{a^b}{p^i} \rfloor}. \end{align*}

But this formula cannot give a way to solve it by hand and even if there was one its going to be large and messy.