Sum digits of big numbers
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Per
el 18 de En. de 2013
Editada: John D'Errico
el 29 de Abr. de 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 comentario
Daniel Shub
el 18 de En. de 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
Respuesta aceptada
Jan
el 18 de En. de 2013
Editada: Jan
el 18 de En. de 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 comentarios
Más respuestas (1)
John D'Errico
el 18 de En. de 2013
Editada: John D'Errico
el 18 de En. de 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 comentarios
John D'Errico
el 19 de En. de 2013
Editada: John D'Errico
el 29 de Abr. de 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.
Ver también
Categorías
Más información sobre Numbers and Precision en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!