Prev: How to make .exe smaller?
Next: preplotting
From: us on 2 Aug 2010 05:00 "Adam Scott" <sportster360(a)yahoo.com> wrote in message <i35th0$juf$1(a)fred.mathworks.com>... > and @ the second poster, thanks for a solution, but I'm not sure what you mean by "if I own the symb tbx" and also I wasn't so much looking for another solution, but more a reason mine doesn't work. It is possibly matlab just wont allow you to store all the digits of a number this big and if so I will have to think of another way to do this. But it seems to me like my way SHOULD be able to work, especially since when I tried to look up someone elses code to try to troubleshoot my own, theirs looked very much like mine and returned my same wrong answer. i thought any solution would be fine and didn't realize you needed help on your particular algorithm... also: sym tbx = symbolic math toolbox sorry for confusion... us
From: John D'Errico on 2 Aug 2010 06:48 "Adam Scott" <sportster360(a)yahoo.com> wrote in message <i35bj8$d6f$1(a)fred.mathworks.com>... > Ok, sorry if this is a stupid question, I'm still kinda new to Matlab. So I am working on some of the problems from Project Euler and got stuck on question 16 which says > > > "2^(15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. > What is the sum of the digits of the number 2^(1000)?" > > > Currently my code for this program is > > > number = 2^1000; > string = num2str(number); > i = 1; > sum = 0; > > while i <= 302 > sum = sum + str2num(string(1,i)); > i = i + 1; > end > > fprintf('The sum of the digits of 2^1000 is %f\n', sum) > > > The problem I am having is that when I enter in number = 2^1000 it puts it in scientific notation with only 15 digits after the decimal point and everything after that 0's. I was then really confused when I looked up someone else's matlab code that supposedly got them the right answer, but when I copied it into my matlab it gave me the same wrong answer. The answer I keep getting is 68 while the real answer is 1366. Any help as to what I'm doing wrong? Thanks for your time. > > ~Adam~ Without the symbolic TB, you might consider VPI as a solution. In fact, vpi has helped me to solve a significant fraction of the 149 problems I've done from PE so far. sum(digits(vpi(2)^1000)) ans = 1366 You can download it from the file exchange, and it has the minor virtue of being free. http://www.mathworks.com/matlabcentral/fileexchange/22725 John
From: Steven_Lord on 3 Aug 2010 10:19
"Adam Scott" <sportster360(a)yahoo.com> wrote in message news:i35th0$juf$1(a)fred.mathworks.com... > @ the first poster, I don't know why I made it print as a floating point. > I guess I got in the habit of printing as a floating point in case it had > something after the decimal. I don't think that should actually be > causing any problems though should it? It would just make my output a > little more sloppy then it should be? > > and @ the second poster, thanks for a solution, but I'm not sure what you > mean by "if I own the symb tbx" and also I wasn't so much looking for > another solution, but more a reason mine doesn't work. It is possibly > matlab just wont allow you to store all the digits of a number this big No. This "problem" is related to IEEE double precision storage. http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf When you get up in the 2^1000 range, the spacing between adjacent representable numbers is greater than 1. In fact, the spacing between the representation of 2^1000 and the next largest representable number is about 2*10^285. >> x = 2^1000 x = 1.07150860718627e+301 >> eps(x) ans = 2.37922705356445e+285 > and if so I will have to think of another way to do this. But it seems to > me like my way SHOULD be able to work, especially since when I tried to > look up someone elses code to try to troubleshoot my own, theirs looked > very much like mine and returned my same wrong answer. Welcome to the world of floating point arithmetic. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com |