Filter löschen
Filter löschen

Cannot return value of a variable

4 Ansichten (letzte 30 Tage)
Ken
Ken am 7 Mär. 2014
Beantwortet: Ken Atwell am 7 Mär. 2014
Hello guys, I am making a program to search for square root of a number, such as x^2 mod p (searching the value of x). However, I cannot return the value of x. Here's my program :
a= 20;
p=29;
r=0;
b=0;
g=0;
%a == mod(a,1)double(uint64(a));
if (mod (a,1) ~= 0) || ~isprime(p)||p <=2
disp('kondisi tdk memenuhi');
elseif (mod (a,1) == 0) && isprime(p)&&p>2 && gcd(a,p)==1
if mod(a^((p-1)/2),p) == mod(-1,p)
disp('tidak ada square root');
elseif mod(a^((p-1)/2),p) == 1
e = 1;
s = (p-1)/(2^e);
while (mod (s,2) == 0)
e=e+1;
s = p-1/(2^e);
end
n = 1;
while mod(n^((p-1)/2),p) ~= 1 || mod(sqrt(n),1) == 0
n = n+1;
end
x = a^(s+1)/2;
b = a^s;
g = n^s;
r = e;
m = 0;
while (m == 0)
m = 0;
while mod(b^(2*m),p)~=1
m = m+1;
end
if m > 0
x = x*g^(2^(r-m-1));
b = b*g^(2^(r-m));
g = g^(2^(r-m));
r = m;
elseif m < 0
disp('tak ada square root');
end
end
end
end
disp(x);
Anyone know what's wrong with my code? because the matlab only says I have an error at "disp(x)". Thanks for your help :D

Antworten (1)

Ken Atwell
Ken Atwell am 7 Mär. 2014
'x' is only assigned if certain conditions are met, so you will get an error with some combinations of 'a' and 'p'.
However, I see what might be an error in logic beginning at 'while (m == 0)': 'm' is next set to 0, so b^(2*m) == b^0, which is always one. So, the 'while mod...' condition is never satisfied and 'm' is never incremented. It would seem that m would remain zero and therefore the while loop would never exit.
I recommend stepping through this code with the debugger to understand what is happening.

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by