I am writing a code that will check two numbers whether they are relatively prime or not if they are it would return 1 else 00the problem is that i am getting 1 all the times. Kindly help me midfy my code.
Show older comments
function [r]=prime(a,b)
a=input('Enter 1st number=');
b=input('Enter 2nd number=');
% factors for 1st number
K=1:a;
D1 = K(rem(a,K)==0)
% factors for 2nd number
K=1:b;
D2 = K(rem(b,K)==0)
n1=length(D1);
n2=length(D2);
for i=2:n1
for o=2:n2
if (D1(i)==D2(o))
r=0;
break
else
if (D1(i)~=D2(o))
r=1;
end
end
end
end
2 Comments
MUTHUKUMAR SETHURAM
on 11 Oct 2017
Edited: Walter Roberson
on 11 Oct 2017
NumbersRange = 47 : 97;
Prime_Index = isprime(NumbersRange);
NumbersRange(Prime_Index)
Divya
on 12 Mar 2023
please share output
Accepted Answer
More Answers (1)
Robert
on 10 Apr 2019
Just do
r=gcd(a,b)==1
instead
Categories
Find more on Loops and Conditional Statements 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!