alexbers / quadratic_sieve

Just a factorization using a quadratic sieve algo
6 stars 3 forks source link

Simplify equation for finding `R_all` in case prime == 2. #2

Closed neizod closed 11 years ago

neizod commented 11 years ago

This part is a bit overkill:

if ( sieve[0] % 2 == 0 and startpoint % 2 == 0 or
     sieve[0] % 2 == 1 and startpoint % 2 == 1 ):
    ...

Python does provide shorthand for checking multiple value at the same time, like:

if ( sieve[0] % 2 == startpoint % 2 == 0 or
     sieve[0] % 2 == startpoint % 2 == 1 ):
    ...

This equation does not yet satisfy me, however. Equality check could be collapse more:

if ( (sieve[0] - startpoint) % 2 == 0 or  # case both equal to 0.
     (sieve[0] - startpoint) % 2 == 0 ):  # case both equal to 1, oops...
    ...

So we now have:

if (sieve[0] - startpoint) % 2 == 0:
    ...

But from very first, we have:

startpoint = int(math.sqrt(N)) - sieving_array_size // 2
sieve[0] = (startpoint + sqrt_N) * (startpoint + sqrt_N) - N

After replace this variable into if-checking make it looks like:

if (startpoint + sqrt_N) ** 2 - N - startpoint) % 2 == 0:
    ...

Expand power-2 and canceling even term:

if (startpoint ** 2 + sqrt_N ** 2 - N - startpoint) % 2 == 0:
    ...

Since we knew that a ** 2 - a == (a - 1) * a and makes this result even, we may cancel startpoint ** 2 - startpoint now:

if (sqrt_N ** 2 - N) % 2 == 0:
    ...

Q.E.D.

alexbers commented 11 years ago

I done some changes in source and this is became even more simple. But I don't sure if it is still quadratic sieve algo.