Why is my modulus messing up?
2 ビュー (過去 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!