MATLAB: How do you save really big integers in the symbolic toolbox

MATLABsymbolictoolbox

I'm trying to write a program which will attempt to factor (very) big numbers. Think more than 64 bits.
I'm using the symbolic toolbox to do this instead of VPI because I need to be able to divide the large integers by two and test if floor(n) = n to see if it's even.
I can't even make it that far in my code however, because when I store the integer to be factored in n, upon recalling n to verify that it stored correctly I can see that it in fact did not.
Any advice? Code is below
n = sym(85098710871039482877157098724987234509871123545)

Best Answer

VPI has no problem dividing a number by 2. Here, for example, is one of my favorite moderately large primes. (That is, it is the smallest value of n, such that (n+1)*17^n-1 is a prime.)
N = 42*vpi(17)^41 - 1;
N
N =
11793835525721281286023333792285550996689383337325513
mod(N,2)
ans =
1
N/2
ans =
5896917762860640643011666896142775498344691668662756
[Q,R] = quotient(N,2)
Q =
5896917762860640643011666896142775498344691668662756
R =
1
You can use quotient divides by 2, also returning the remainder of that integer divide.
But, if you absolutely need to use syms...
N = 42*sym(17)^41 - 1
N =
11793835525721281286023333792285550996689383337325513
mod(N,2)
ans =
1
The reason you had a problem with sym is because you used it improperly. When you did this:
n = sym(85098710871039482877157098724987234509871123545)
you made a mistake. When you type it like this:
n = sym(85098710871039482877157098724987234509871123545)
n =
85098710871039487337987513201467377652091322368
MATLAB does NOT look at what you will be doing with the number 85098710871039482877157098724987234509871123545. It converts it to a DOUBLE, so roughly 16 decimal digits. Then it passes that to sym, so you get garbage. Remember that when you want a number to be interpreted NOT as a double, you cannot let MATLAB create the number as a double. Both VPI and SYM have a simple way to avoid that problem.
See the difference here:
n = sym('85098710871039482877157098724987234509871123545')
n =
85098710871039482877157098724987234509871123545
In fact, VPI has the same problem. You cannot just pass that number to VPI as a double. syms actually has no problem with factoring that number.
factor(sym('85098710871039482877157098724987234509871123545'))
ans =
[ 3, 5, 59, 144864327936049, 663770969827375707224424962533]
In fact, sym is quicker than is VPI on that number, in fact, syms even beats my replacement for VPI, thus is much faster than VPIJ in factoring. (One thing I always wanted to improve. One day I'll get a nice quadratic sieve code running for VPIJ.)
Related Question