However, realmax is NOT the limit of a double such that it will represent an exact integer. That limit is 2^53. Your product exceeds that limit.
For example, we see that when a double exceeds 2^53 (called flintmax), the double is not represented down to the least significant bit. For example, we can add 1 to 2^54, and get the same number.
2^54 == 2^54 + 1
ans =
logical
1
You could have used a big integer form, for exampel, my VPI toolbox.
A = vpi([32 27 25 7 11 13 17 19 23 29 31 37 41 43]);
mod(prod(A),27)
ans =
0
Or use sym.
A = sym([32 27 25 7 11 13 17 19 23 29 31 37 41 43]);
mod(prod(A),27)
ans =
0
In either case, you needed a tool that could handle the large integers involved.
Since the product of your numbers is less than the limit of uint64, one might think you could use uint64 to do this.
A = uint64([32 27 25 7 11 13 17 19 23 29 31 37 41 43]);
class(prod(A))
ans =
double
So it looks like the prod of a uint64 array is still returned as a double.
One other thing you could have done, which works since many of the elements in A are at least 27, or larger:
A = [32 27 25 7 11 13 17 19 23 29 31 37 41 43]
mod(prod(mod(A,27)),27)
ans =
0
In fact, since one element is exactly 27, this too will work:
Best Answer