Why is my modulus messing up?
1 次查看(过去 30 天)
显示 更早的评论
Here is my code, it is my own take on creating Pollard's method using Matlab. For most numbers it works great, but if you use a number such as 1234567, it returns that it is divisible by 1234567, but due to the modulus, the number itself should never even show up. (By the way, 1234567 is not prime (127) is a divisor).
function Pollards_div(k)
z = @(x) x.^2 - 7;
y(1) = 2;
m = 1;
for i = 2:21
y(i) = z(y(i-1));
end
while m < 11
q = mod(y(m)-y(2*m), k);
G = gcd(q,k);
if G > 1
disp([num2str(k) ' is divisible by ' num2str(G)]);
m = m + 10;
else
m = m+1;
end
end
end
Please help
0 个评论
回答(1 个)
John D'Errico
2015-8-2
编辑:John D'Errico
2015-8-2
Almost always one of the iterations here will have ended up exceeding 2^53-1. That is the limit for a floating point double to be an integer. In that case, you should expect INTEGER arithmetic to fail.
2 个评论
John D'Errico
2015-8-2
gcd(0,k) or gcd(k,0) will always return abs(k). This is strongly implied by the help, although not explicitly stated as such, and it is the only logical result. (Ok, I would have stated that fact, IF I had written the help.)
The help does state that all other GCDs besides gcd(0,0) will return a positive integer, and that gcd(0,0) returns 0.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!