Closed neizod closed 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:
a ** 2 - a == (a - 1) * a
startpoint ** 2 - startpoint
if (sqrt_N ** 2 - N) % 2 == 0: ...
Q.E.D.
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.
This part is a bit overkill:
Python does provide shorthand for checking multiple value at the same time, like:
This equation does not yet satisfy me, however. Equality check could be collapse more:
So we now have:
But from very first, we have:
After replace this variable into if-checking make it looks like:
Expand power-2 and canceling even term:
Since we knew that
a ** 2 - a == (a - 1) * a
and makes this result even, we may cancelstartpoint ** 2 - startpoint
now:Q.E.D.