Prev: How to make .exe smaller?
Next: preplotting
From: Adam Scott on 1 Aug 2010 22:47 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~
From: Walter Roberson on 1 Aug 2010 23:16 Adam Scott wrote: > 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; That calculates 2^1000 as a binary floating point value. In order to use VPA you need to work with symbolic numbers: number = sym('2^1000'); > string = num2str(number); I don't know for sure whether num2str() works on symbolic objects. I suspect that if it works at all it would convert to binary floating point first. char(number) should, however, produce the number in its full digits. > i = 1; > sum = 0; > > while i <= 302 Why 302? Shouldn't you use length(char(number)) ? > sum = sum + str2num(string(1,i)); str2num() of a single character is functionally equivalent to taking the character and subtracting the character '0' . > i = i + 1; Why not use a "for" loop instead of a while? > end > > fprintf('The sum of the digits of 2^1000 is %f\n', sum) Why use floating point to print out the sum when the sum will be integral ?
From: us on 2 Aug 2010 03:04 "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~ one of the solutions - note: if you own the symb tbx r=sum(strread(char(sym('2^1000')),'%c')-'0') % r = 1366 us
From: Adam Scott on 2 Aug 2010 03:53 @ 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 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.
From: Steve Amphlett on 2 Aug 2010 04:50
"Adam Scott" <sportster360(a)yahoo.com> wrote in message <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 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. Here is a loopy version that solves the specific problem (2^N). N=1000; x=zeros(1,ceil(N*log(2)/log(10))); x(1)=1; for n=1:N x=2*x; idx=find(x>=10); x(idx)=x(idx)-10; x(idx+1)=x(idx+1)+1; end sum(x) |