Sum digits of big numbers

3 views (last 30 days)
Per
Per on 18 Jan 2013
Edited: John D'Errico on 29 Apr 2014
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!
  1 Comment
Daniel Shub
Daniel Shub on 18 Jan 2013
Please do not embed your code as an image, but rather copy/paste it so we can copy/paste it. Please edit and reopen

Sign in to comment.

Accepted Answer

Jan
Jan on 18 Jan 2013
Edited: Jan on 18 Jan 2013
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.
  3 Comments
Jan
Jan on 18 Jan 2013
Edited: Jan 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
Per on 18 Jan 2013
Thank you Jan, it works now :)

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 18 Jan 2013
Edited: 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
  2 Comments
Per
Per on 18 Jan 2013
Great answer, thank you!
John D'Errico
John D'Errico on 19 Jan 2013
Edited: John D'Errico on 29 Apr 2014
Note that I usd a couple of tools that I've put on the file exchange, vpi, and max2. max2 is not really necessary, but it makes things easier. And the vpi usage can be replaced with the symbolic TB, if you have that.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!