matlab precision dealing with big number












1














The question encountered me when I was trying to solve Project Euler #16.



The problem reads:




215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.



What is the sum of the digits of the number 21000?




I was trying to deal with it with MATLAB. Here's my code:



function [sum] = power_digit_sum_p16(pow)
sum = 0;
n = 2 ^ pow;
M = zeros(1,301);
for i = 301 : -1 : 0
a = floor(n / 10 ^ i);
M(302 - i) = a;
sum = sum + a;
n = n - 10 ^ i * a;
end
end


(Where the 301 stands for 301 digits of 2^1000)



When I ran pow_digit_sum_p16(1000), MATLAB returned 1285, which is wrong.



Then I checked the number provided by MATLAB, which is stored in matrix M, when I learned that something is wrong.



The last digit of 2^1000 should be 6 instead of 2 (it follows a 2-4-8-6-2-4-8-6 pattern)! I don't understand what's going on here with MATLAB, but I guess the problem occurred because the number is too big, since my function works fine when pow is small.



My friend provided me with a Python solution, and it seems that python handles the big number well. Below is the code in Python:



x=2**1000
ans=0
while x>0:
ans+=(x%10)
x=x//10
print(ans)


Update: thanks to OmG's answer, I changed my code a little bit:



function [sum] = power_digit_sum_p16(pow)
sum = 0;
n = 2 ^ pow;
n = sym(n); % This line is new!
M = zeros(1,301);
for i = 301 : -1 : 0
a = floor(n / (10 ^ i));
M(302 - i) = a;
sum = sum + a;
n = n - (10 ^ i) * a;
end
end


And I got what I expected. Note that the sym function needs the Symbolic Math Toolbox.










share|improve this question
























  • I assume it's related to the precision of the number that is related to the problem you've described above.
    – Nahiyan
    Dec 28 '18 at 7:45










  • So any idea to do to increase the precision in MATLAB computation? The fact that python handles it well make me kind of "jealous".
    – Frank Li
    Dec 28 '18 at 7:49












  • Use the repeating pattern property, read here:en.wikipedia.org/wiki/Power_of_two "The 0th through 95th powers of two"
    – Mendi Barel
    Dec 28 '18 at 9:56








  • 1




    Python has arbitrary-precision integers. Matlab doesn't, unless you use symbolic computations
    – Luis Mendo
    Dec 28 '18 at 11:02








  • 2




    Isn't the whole point of the problem to compute the sum of the digits without an explicit calculation of 2^1000 ? Project Euler is about cunning, not about brute force.
    – High Performance Mark
    Dec 28 '18 at 11:15
















1














The question encountered me when I was trying to solve Project Euler #16.



The problem reads:




215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.



What is the sum of the digits of the number 21000?




I was trying to deal with it with MATLAB. Here's my code:



function [sum] = power_digit_sum_p16(pow)
sum = 0;
n = 2 ^ pow;
M = zeros(1,301);
for i = 301 : -1 : 0
a = floor(n / 10 ^ i);
M(302 - i) = a;
sum = sum + a;
n = n - 10 ^ i * a;
end
end


(Where the 301 stands for 301 digits of 2^1000)



When I ran pow_digit_sum_p16(1000), MATLAB returned 1285, which is wrong.



Then I checked the number provided by MATLAB, which is stored in matrix M, when I learned that something is wrong.



The last digit of 2^1000 should be 6 instead of 2 (it follows a 2-4-8-6-2-4-8-6 pattern)! I don't understand what's going on here with MATLAB, but I guess the problem occurred because the number is too big, since my function works fine when pow is small.



My friend provided me with a Python solution, and it seems that python handles the big number well. Below is the code in Python:



x=2**1000
ans=0
while x>0:
ans+=(x%10)
x=x//10
print(ans)


Update: thanks to OmG's answer, I changed my code a little bit:



function [sum] = power_digit_sum_p16(pow)
sum = 0;
n = 2 ^ pow;
n = sym(n); % This line is new!
M = zeros(1,301);
for i = 301 : -1 : 0
a = floor(n / (10 ^ i));
M(302 - i) = a;
sum = sum + a;
n = n - (10 ^ i) * a;
end
end


And I got what I expected. Note that the sym function needs the Symbolic Math Toolbox.










share|improve this question
























  • I assume it's related to the precision of the number that is related to the problem you've described above.
    – Nahiyan
    Dec 28 '18 at 7:45










  • So any idea to do to increase the precision in MATLAB computation? The fact that python handles it well make me kind of "jealous".
    – Frank Li
    Dec 28 '18 at 7:49












  • Use the repeating pattern property, read here:en.wikipedia.org/wiki/Power_of_two "The 0th through 95th powers of two"
    – Mendi Barel
    Dec 28 '18 at 9:56








  • 1




    Python has arbitrary-precision integers. Matlab doesn't, unless you use symbolic computations
    – Luis Mendo
    Dec 28 '18 at 11:02








  • 2




    Isn't the whole point of the problem to compute the sum of the digits without an explicit calculation of 2^1000 ? Project Euler is about cunning, not about brute force.
    – High Performance Mark
    Dec 28 '18 at 11:15














1












1








1







The question encountered me when I was trying to solve Project Euler #16.



The problem reads:




215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.



What is the sum of the digits of the number 21000?




I was trying to deal with it with MATLAB. Here's my code:



function [sum] = power_digit_sum_p16(pow)
sum = 0;
n = 2 ^ pow;
M = zeros(1,301);
for i = 301 : -1 : 0
a = floor(n / 10 ^ i);
M(302 - i) = a;
sum = sum + a;
n = n - 10 ^ i * a;
end
end


(Where the 301 stands for 301 digits of 2^1000)



When I ran pow_digit_sum_p16(1000), MATLAB returned 1285, which is wrong.



Then I checked the number provided by MATLAB, which is stored in matrix M, when I learned that something is wrong.



The last digit of 2^1000 should be 6 instead of 2 (it follows a 2-4-8-6-2-4-8-6 pattern)! I don't understand what's going on here with MATLAB, but I guess the problem occurred because the number is too big, since my function works fine when pow is small.



My friend provided me with a Python solution, and it seems that python handles the big number well. Below is the code in Python:



x=2**1000
ans=0
while x>0:
ans+=(x%10)
x=x//10
print(ans)


Update: thanks to OmG's answer, I changed my code a little bit:



function [sum] = power_digit_sum_p16(pow)
sum = 0;
n = 2 ^ pow;
n = sym(n); % This line is new!
M = zeros(1,301);
for i = 301 : -1 : 0
a = floor(n / (10 ^ i));
M(302 - i) = a;
sum = sum + a;
n = n - (10 ^ i) * a;
end
end


And I got what I expected. Note that the sym function needs the Symbolic Math Toolbox.










share|improve this question















The question encountered me when I was trying to solve Project Euler #16.



The problem reads:




215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.



What is the sum of the digits of the number 21000?




I was trying to deal with it with MATLAB. Here's my code:



function [sum] = power_digit_sum_p16(pow)
sum = 0;
n = 2 ^ pow;
M = zeros(1,301);
for i = 301 : -1 : 0
a = floor(n / 10 ^ i);
M(302 - i) = a;
sum = sum + a;
n = n - 10 ^ i * a;
end
end


(Where the 301 stands for 301 digits of 2^1000)



When I ran pow_digit_sum_p16(1000), MATLAB returned 1285, which is wrong.



Then I checked the number provided by MATLAB, which is stored in matrix M, when I learned that something is wrong.



The last digit of 2^1000 should be 6 instead of 2 (it follows a 2-4-8-6-2-4-8-6 pattern)! I don't understand what's going on here with MATLAB, but I guess the problem occurred because the number is too big, since my function works fine when pow is small.



My friend provided me with a Python solution, and it seems that python handles the big number well. Below is the code in Python:



x=2**1000
ans=0
while x>0:
ans+=(x%10)
x=x//10
print(ans)


Update: thanks to OmG's answer, I changed my code a little bit:



function [sum] = power_digit_sum_p16(pow)
sum = 0;
n = 2 ^ pow;
n = sym(n); % This line is new!
M = zeros(1,301);
for i = 301 : -1 : 0
a = floor(n / (10 ^ i));
M(302 - i) = a;
sum = sum + a;
n = n - (10 ^ i) * a;
end
end


And I got what I expected. Note that the sym function needs the Symbolic Math Toolbox.







matlab precision






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 7:40









Cris Luengo

19.1k51947




19.1k51947










asked Dec 28 '18 at 7:43









Frank LiFrank Li

83




83












  • I assume it's related to the precision of the number that is related to the problem you've described above.
    – Nahiyan
    Dec 28 '18 at 7:45










  • So any idea to do to increase the precision in MATLAB computation? The fact that python handles it well make me kind of "jealous".
    – Frank Li
    Dec 28 '18 at 7:49












  • Use the repeating pattern property, read here:en.wikipedia.org/wiki/Power_of_two "The 0th through 95th powers of two"
    – Mendi Barel
    Dec 28 '18 at 9:56








  • 1




    Python has arbitrary-precision integers. Matlab doesn't, unless you use symbolic computations
    – Luis Mendo
    Dec 28 '18 at 11:02








  • 2




    Isn't the whole point of the problem to compute the sum of the digits without an explicit calculation of 2^1000 ? Project Euler is about cunning, not about brute force.
    – High Performance Mark
    Dec 28 '18 at 11:15


















  • I assume it's related to the precision of the number that is related to the problem you've described above.
    – Nahiyan
    Dec 28 '18 at 7:45










  • So any idea to do to increase the precision in MATLAB computation? The fact that python handles it well make me kind of "jealous".
    – Frank Li
    Dec 28 '18 at 7:49












  • Use the repeating pattern property, read here:en.wikipedia.org/wiki/Power_of_two "The 0th through 95th powers of two"
    – Mendi Barel
    Dec 28 '18 at 9:56








  • 1




    Python has arbitrary-precision integers. Matlab doesn't, unless you use symbolic computations
    – Luis Mendo
    Dec 28 '18 at 11:02








  • 2




    Isn't the whole point of the problem to compute the sum of the digits without an explicit calculation of 2^1000 ? Project Euler is about cunning, not about brute force.
    – High Performance Mark
    Dec 28 '18 at 11:15
















I assume it's related to the precision of the number that is related to the problem you've described above.
– Nahiyan
Dec 28 '18 at 7:45




I assume it's related to the precision of the number that is related to the problem you've described above.
– Nahiyan
Dec 28 '18 at 7:45












So any idea to do to increase the precision in MATLAB computation? The fact that python handles it well make me kind of "jealous".
– Frank Li
Dec 28 '18 at 7:49






So any idea to do to increase the precision in MATLAB computation? The fact that python handles it well make me kind of "jealous".
– Frank Li
Dec 28 '18 at 7:49














Use the repeating pattern property, read here:en.wikipedia.org/wiki/Power_of_two "The 0th through 95th powers of two"
– Mendi Barel
Dec 28 '18 at 9:56






Use the repeating pattern property, read here:en.wikipedia.org/wiki/Power_of_two "The 0th through 95th powers of two"
– Mendi Barel
Dec 28 '18 at 9:56






1




1




Python has arbitrary-precision integers. Matlab doesn't, unless you use symbolic computations
– Luis Mendo
Dec 28 '18 at 11:02






Python has arbitrary-precision integers. Matlab doesn't, unless you use symbolic computations
– Luis Mendo
Dec 28 '18 at 11:02






2




2




Isn't the whole point of the problem to compute the sum of the digits without an explicit calculation of 2^1000 ? Project Euler is about cunning, not about brute force.
– High Performance Mark
Dec 28 '18 at 11:15




Isn't the whole point of the problem to compute the sum of the digits without an explicit calculation of 2^1000 ? Project Euler is about cunning, not about brute force.
– High Performance Mark
Dec 28 '18 at 11:15












1 Answer
1






active

oldest

votes


















1














A solution is using symbolic tools. For example for finding the true value for the remaining for 2^1000 over 10 you can do it likes the following:



x = sym('2^1000');
reminder = fix(double(mod(x,10)))


The answer is:



 reminder = 
6


For 100 and 1000 is 67 and 376 respectively.






share|improve this answer























  • The only thing wrong with this answer is that it is not an answer to the question asked.
    – High Performance Mark
    Dec 28 '18 at 18:12










  • Your is the answer I wanted, thank you!
    – Frank Li
    Dec 29 '18 at 16:32










  • Something to improve in your answer, 1.solutio--->solution(a typo) 2. x=sym(2^1000), do not make it a string.
    – Frank Li
    Dec 29 '18 at 20:32













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53955228%2fmatlab-precision-dealing-with-big-number%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














A solution is using symbolic tools. For example for finding the true value for the remaining for 2^1000 over 10 you can do it likes the following:



x = sym('2^1000');
reminder = fix(double(mod(x,10)))


The answer is:



 reminder = 
6


For 100 and 1000 is 67 and 376 respectively.






share|improve this answer























  • The only thing wrong with this answer is that it is not an answer to the question asked.
    – High Performance Mark
    Dec 28 '18 at 18:12










  • Your is the answer I wanted, thank you!
    – Frank Li
    Dec 29 '18 at 16:32










  • Something to improve in your answer, 1.solutio--->solution(a typo) 2. x=sym(2^1000), do not make it a string.
    – Frank Li
    Dec 29 '18 at 20:32


















1














A solution is using symbolic tools. For example for finding the true value for the remaining for 2^1000 over 10 you can do it likes the following:



x = sym('2^1000');
reminder = fix(double(mod(x,10)))


The answer is:



 reminder = 
6


For 100 and 1000 is 67 and 376 respectively.






share|improve this answer























  • The only thing wrong with this answer is that it is not an answer to the question asked.
    – High Performance Mark
    Dec 28 '18 at 18:12










  • Your is the answer I wanted, thank you!
    – Frank Li
    Dec 29 '18 at 16:32










  • Something to improve in your answer, 1.solutio--->solution(a typo) 2. x=sym(2^1000), do not make it a string.
    – Frank Li
    Dec 29 '18 at 20:32
















1












1








1






A solution is using symbolic tools. For example for finding the true value for the remaining for 2^1000 over 10 you can do it likes the following:



x = sym('2^1000');
reminder = fix(double(mod(x,10)))


The answer is:



 reminder = 
6


For 100 and 1000 is 67 and 376 respectively.






share|improve this answer














A solution is using symbolic tools. For example for finding the true value for the remaining for 2^1000 over 10 you can do it likes the following:



x = sym('2^1000');
reminder = fix(double(mod(x,10)))


The answer is:



 reminder = 
6


For 100 and 1000 is 67 and 376 respectively.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 30 '18 at 6:50

























answered Dec 28 '18 at 13:14









OmGOmG

7,89352643




7,89352643












  • The only thing wrong with this answer is that it is not an answer to the question asked.
    – High Performance Mark
    Dec 28 '18 at 18:12










  • Your is the answer I wanted, thank you!
    – Frank Li
    Dec 29 '18 at 16:32










  • Something to improve in your answer, 1.solutio--->solution(a typo) 2. x=sym(2^1000), do not make it a string.
    – Frank Li
    Dec 29 '18 at 20:32




















  • The only thing wrong with this answer is that it is not an answer to the question asked.
    – High Performance Mark
    Dec 28 '18 at 18:12










  • Your is the answer I wanted, thank you!
    – Frank Li
    Dec 29 '18 at 16:32










  • Something to improve in your answer, 1.solutio--->solution(a typo) 2. x=sym(2^1000), do not make it a string.
    – Frank Li
    Dec 29 '18 at 20:32


















The only thing wrong with this answer is that it is not an answer to the question asked.
– High Performance Mark
Dec 28 '18 at 18:12




The only thing wrong with this answer is that it is not an answer to the question asked.
– High Performance Mark
Dec 28 '18 at 18:12












Your is the answer I wanted, thank you!
– Frank Li
Dec 29 '18 at 16:32




Your is the answer I wanted, thank you!
– Frank Li
Dec 29 '18 at 16:32












Something to improve in your answer, 1.solutio--->solution(a typo) 2. x=sym(2^1000), do not make it a string.
– Frank Li
Dec 29 '18 at 20:32






Something to improve in your answer, 1.solutio--->solution(a typo) 2. x=sym(2^1000), do not make it a string.
– Frank Li
Dec 29 '18 at 20:32




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53955228%2fmatlab-precision-dealing-with-big-number%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas