필터 지우기
필터 지우기

Sum digits of big numbers

조회 수: 1 (최근 30일)
Per
Per 2013년 1월 18일
편집: John D'Errico 2014년 4월 29일
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
Daniel Shub
Daniel Shub 2013년 1월 18일
Please do not embed your code as an image, but rather copy/paste it so we can copy/paste it. Please edit and reopen

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2013년 1월 18일
편집: Jan 2013년 1월 18일
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
Jan
Jan 2013년 1월 18일
편집: Jan 2013년 1월 18일
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 2013년 1월 18일
Thank you Jan, it works now :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John D'Errico
John D'Errico 2013년 1월 18일
편집: John D'Errico 2013년 1월 18일
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
Per
Per 2013년 1월 18일
Great answer, thank you!
John D'Errico
John D'Errico 2013년 1월 19일
편집: John D'Errico 2014년 4월 29일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by