我已经掌握了整数的质因子分解,但现在我想为高斯整数实现它,但应该如何做呢?谢谢!
我已经掌握了整数的质因子分解,但现在我想为高斯整数实现它,但应该如何做呢?谢谢!
For each remaining factor p:
if p=2, u = (1 + i).
strike p from the list of remaining primes.
else if p mod 4 = 3, q = p, and strike 2 copies of p from the list of primes.
else find k such that k^2 = -1 mod p, then u = gcd(p, k+i)
if G/u has remainder 0, q = u
else q = conjugate(u)
strike p from the list of remaining primes.
Add q to the list of Gaussian factors.
Replace G with G/q.
endfor
考虑到17,它也与1 mod 4同余,因此我们需要找到另一个k mod 17。尝试n=2,28 = 1 mod 17,不行。改为尝试n=3。38 = 16 mod 17 = -1 mod 17。成功!所以k = 34 = 13 mod 17。 gcd(17, 13+i) = u = 4-i,G/u = -99 -96i,余数-2。不行,尝试u的共轭=4+i。G/u = -133 - 38i,余数0。又得到一个因子!
G <= G/(4+i) = -133 - 38i
考虑到19,它与3 mod 4同余。因此我们的下一个因子是19,并从列表中删除第二个19。
G <= G/19 = -7 - 2i
考虑到53,它与4模同余。再进行k过程... 尝试n=2,2的26次方=52模53=-1模53。那么k=2的13次方模53=30。 gcd(53,30+i)=u=-7-2i。这与G相同,因此最终商G/(-7-2i)=1,无需担心-1、i或-i的因数问题。 我们已找到因子(1+i)(2+i)(4+i)(19+0i)(-7-2i)。如果你把它乘起来(留给读者自己练习),你会发现积为361-1767i,这就是我们开始的数字。 数论不是很棒吗?1+i = i(1-i)
,所以一个数可以被1+i整除当且仅当它能被1-i整除,如果它能被2整除,那么它就能同时被这两个数整除,所以在那里无需检查可除性。 - davin如果您想要完整的单元格整数精度,请使用浮点数表示实部和虚部,并定义gsub、gmul和一个特殊的带有舍入系数而非向下取整的gdivr除法。这是因为Pollard rho分解方法需要通过欧几里得算法进行gcd计算,其中稍微修改了gmodulo:
gmodulo((x,y),(x',y'))=gsub((x,y),gmul((x',y'),gdivr((x,y),(x',y'))))
波拉德-洛算法
def poly((a,b),(x,y))=gmodulo(gsub(gmul((a,b),(a,b)),(1,0)),(x,y))
input (x,y),(a,b) % (x,y) is the Gaussian number to be factorized
(c,d)<-(a,b)
do
(a,b)=poly((a,b),(x,y))
(c,d)=poly(poly((c,d),(x,y)),(x,y))
(e,f)=ggcd((x,y),gsub((a,b),(c,d)))
if (e,f)=(x,y) then return (x,y) % failure, try other (a,b)
until e^2+f^2>1
return (e,f)
一个正常的起始值是a=1,b=0。
我已经在我的博客http://forthmath.blogspot.se中使用了这种方法来编程Forth。
为了安全起见,在使用浮点数进行整数计算时,请在所有计算中使用舍入值。