Why is my modulus messing up?
5 views (last 30 days)
Show older comments
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 Comments
Answers (1)
John D'Errico
on 2 Aug 2015
Edited: John D'Errico
on 2 Aug 2015
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 Comments
John D'Errico
on 2 Aug 2015
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.
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!