Asked by Per
on 18 Jan 2013

Hi,

I'm trying to solve the following problem:

for a^b when 0 < a,b < 100 I want to find the maximum sum of digits of the generated numbers. For example 9^3=729, sum=7+2+9=18.

I have the following code:

clear all clc digits(1000) tot=0; for a=99:-1:1 for b=99:-1:1 Num=vpa(sym(a^b)); dig=double(1+floor(log10(Num)+eps)); Num=Num/10^dig; sumA=0; for i=1:dig+1 sumA = sumA+(floor(Num*(10^(i-1)))-10*floor(Num*10^(i-2))); end temp=double(sumA); if temp == 972 disp(a) disp(b) end end end

Where I found that 88^99 yields the max sum of digits 978, but it's the wrong answer. (correct answer is 972). Does anyone find the error?

Cheers!

*No products are associated with this question.*

Answer by Jan Simon
on 18 Jan 2013

Edited by Jan Simon
on 18 Jan 2013

Accepted Answer

Does `sym(a^b)` reply a symbolic variable? I'd assume, that at first `a^b` is calculated as double with the corresponding rounding errors. What about `sym('a^b')`? I do not have the required toolbox, such that this is a guessing only.

Per
on 18 Jan 2013

If I use sym('a^b') with the '' the loops doesn't work, a and b change value each turn. and sym(a^b) has worked before without rounding errors, or perhaps those times have been a fluke :O

How can I use sym('a^b') with for loops?

Jan Simon
on 18 Jan 2013

I would not use the loop counters of tyoe `double` directly in the expression, which must be symbolic. What about:

Num = power(vpa(a), vpa(b))

? If the POWER operation is not defined for VPA numbers, write a corresponding function is easy.

Per
on 18 Jan 2013

Thank you Jan, it works now :)

Log in to comment.

Answer by John D'Errico
on 18 Jan 2013

Edited by John D'Errico
on 18 Jan 2013

Well, I'd use my vpi tool. The problem is, the number is far too large for all those digits to be represented using a standard floating point form like a double.

(Actually, I'd use the replacement for it, that is essentially written, subject to heavy use for testing.)

vpi(88).^99 ans = 31899548991064687385194313314353745484864573065650712770111884048604 753593728365505650462765416702028265157186333205198215936166634716861519 60018780508843851702573924250277584030257178740785152

As a test, compare the symbolic toolbox:

sym(88)^99 ans = 3189954899106468738519431331435374548486457306565071277011188404860475359372836550565046276541670202826515718633320519821593616663471686151960018780508843851702573924250277584030257178740785152

Yep, they agree down to the last digit. The sum of those digits is 847.

sum(digits(vpi(88).^99)) ans = 847

Or, do it using the symbolic TB.

sum(char(sym(88)^99)-'0') ans = 847

Either works, and yields 847. See that I was careful to do the exponentiation on either a symbolic starting point or a vpi start. Otherwise, MATLAB forms the result 88^99 as a double, THEN tries to convert it to a high precision result. Of course that must fail.

So how do we find the maximum sum? This seems to work, using vpi.

[A,B] = ndgrid(1:99); A = vpi(A); B = vpi(B); C = A.^B; CC = mat2cell(C,ones(1,99),ones(1,99));

sumfun = @(N) sum(digits(N)); dsum = cellfun(sumfun,CC); [S,loc] = max2(dsum)

S = 972

loc = 99 95

So we learn that 99^95 is

vpi(99).^95 ans = 38489607889348486119277958028245967896084511560873660346586279535301 481260085342580322673837686274870946109685542866926973747267258531956576 79460590239636893953692985541958490801973870359499

With a sum of digits:

sum(digits(vpij(99).^95)) ans = 972

Per
on 18 Jan 2013

Great answer, thank you!

John D'Errico
on 19 Jan 2013

Log in to comment.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 1 Comment

## Daniel Shub (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/59178-sum-digits-of-big-numbers#comment_123387

Please do not embed your code as an image, but rather copy/paste it so we can copy/paste it. Please edit and reopen

Log in to comment.