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.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Frank Li
  • 55
  • 5
  • 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 07: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 07:49
  • Use the repeating pattern property, read here:https://en.wikipedia.org/wiki/Power_of_two "The 0th through 95th powers of two" – Mendi Barel Dec 28 '18 at 09: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
  • Hi, High Performance Mark. I got your point, yours suggestion is absolutely right. What make me wonder here is similar algorithm work in Python but not MATLAB. If Python's integer can be as precise as wanted, then I have to admit Python has some advantages here over MATLAB. – Frank Li Dec 29 '18 at 04:28
  • MATLAB also has an arbitrary precision number class (`vpa`). It comes with the symbolic toolbox I think. – Cris Luengo Dec 29 '18 at 17:04
  • Related: https://stackoverflow.com/questions/21294581/calculate-floorpow2-n-10-mod-10-sum-of-digits-of-pow2-n – Cris Luengo Dec 29 '18 at 17:04
  • Also interesting: https://math.stackexchange.com/questions/2006159/leading-digits-of-large-power-of-2 – Cris Luengo Dec 29 '18 at 17:07

1 Answers1

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.

OmG
  • 18,337
  • 10
  • 57
  • 90
  • 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