5

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.

Juan Zamora
  • 386
  • 4
  • 11

2 Answers2

11

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
Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • @JuanZamora ¡Un placer! :-) – Luis Mendo Apr 26 '15 at 23:35
  • Your example is using full symbolic math, not variable precision. There doesn't seem to be any reason to use variable precision here. Using variable precision with `vpa` means you have a finite, but adjustable, number of digits of precision. Maybe I don't understand what you're trying to say – you don't lose precision with respect to your specified value for `digits`, but in manu cases you absolutely can lose precision relative to the true value using `vpa`. – horchler Apr 27 '15 at 00:32
  • 1
    @LuisMendo check this one: 79^-1 mod 3220 = 1019 in wolfram. In matlab is just returning 1/79 = 0.0126... I tried the symbolic was, but no luck. – Juan Zamora Apr 27 '15 at 01:29
  • 1
    @JuanZamora That's a little more nuanced. WolframAlpha appears to be solving the [modular inverse problem](http://mathworld.wolfram.com/ModularInverse.html) for the input `79^-1 mod 3220` (interpreted as `PowerMod[79,-1,3220]` but will solve the standard remainder problem for `1/79 mod 3220` (interpreted as `Mod[79^-1,3220]`). – TroyHaskin Apr 27 '15 at 05:19
  • @horchler You are completely right. I was trying several things and ended up not using `vpa`, but the explanation was wrong. Thanks! I've corrected it – Luis Mendo Apr 27 '15 at 09:09
3

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.

Cedric
  • 117
  • 1
  • 12
  • 1
    for the record your number is 147736096754175303230053677431108078993422779056521621788516135638187994995899192814258977221160914022825628299180402236602527982600775671266557420283463691946979929718178787089203370822514764608144873171356044647950190641152 Full stop – Cedric Apr 26 '15 at 23:37