4
votes

688^79 mod 3337 = 1570.

When I tried this at wolfram alpha I got: enter image description here

but When I entered the same thing in Matlab, I get 364 as the answer. I got to be doing something wrong.

enter image description here

Any light on this will be appreciated.

2
Some Overflow perhaps? - codingEnthusiast
might be, let me run a quick test with a smaller value - Juan Zamora
with mod(4^3,24) both wolfram and matlab shows the same result == 16 - Juan Zamora
1570 is correct (calculated with python). Try mod(double(688^79), 3337) - codingEnthusiast
Just store them as sym and you will be able to calculate it correctly. - codingEnthusiast

2 Answers

11
votes

The reason is that Matlab uses double floating-point arithmetic by default. A number as large as 688^79 can't be represented accurately as a double. (The largest integer than can be accurately represented as a double is of the order of 2^53).

To obtain the right result you can use symbolic variables, which ensures you don't lose accuracy:

>> x = sym('688^79');
>> y = sym('3337');
>> mod(x, y)
ans =
1570
3
votes

My calculator is sending me the same answer than Wolfram, it also calculated the value for 688^79 so I would tend to believe Wolfram is right. You probably have overrun the capacities of Matlab with such a huge number and it is why it did not send the right answer.