How to show non prime numbers (what is the contrari function of prime?)

1 次查看(过去 30 天)
clc
clear all
n=input('number') % Natural number that you want to know if it a prime number
i=2;
x=[];
if n==0 | n==1
disp('there are not prime numbers');
end
while i<=sqrt(n)
if rem(n,i)==0 %In this part i want to show the non prime numbers (i don't know the function name)
x=n
else %In this part i show the prime numbers
y=primes(n)
break
end
i=i+1;
end
fid=fopen('Prime_numbers.txt','w'); %In this final part i create a .txt document for save the prime
fprintf(fid,'%2d\t%2g\t',y);
fclose(fid);
fid=fopen('Non_prime_numbers.txt','w'); %In this final part i create a .txt document for save the non prime
fprintf(fid,'%2d\t%2g\t',x);
fclose(fid);

采纳的回答

Walter Roberson
Walter Roberson 2016-10-9
You could use setdiff() of the range of values, asking to remove the primes.
  2 个评论
Toni Clares
Toni Clares 2016-10-9
编辑:Walter Roberson 2016-10-9
%Thanks i tried but i don't know the right way to use it
clc
clear all
n=input('number') % Natural number that you want to know if it a prime number
i=2;
x=[];
if n==0 | n==1
x=n;
end
while i<=sqrt(n)
if rem(n,i)==0
x=n
else
z=1:n
y=primes(n)
x=setfdiff(z,y) % I don't know how to use it
break
end
i=i+1;
end
fid=fopen('Prime_numbers.txt','w');
fprintf(fid,'%2d\t%2g\t',y);
fclose(fid);
fid=fopen('Non_prime_numbers.txt','w');
fprintf(fid,'%2d\t%2g\t',x);
fclose(fid);
Toni Clares
Toni Clares 2016-10-9
编辑:Walter Roberson 2016-10-9
I sort out!!!
clc
clear all
n=input('number') % Natural number that you want to know if it a prime number
i=2;
x=[];
if n==0 | n==1
x=n;
end
z=1:n;
y=primes(n)
x=setdiff(z,y)
fid=fopen('Prime_numbers.txt','w');
fprintf(fid,'%2d\t%2g\t',y);
fclose(fid);
fid=fopen('Non_prime_numbers.txt','w');
fprintf(fid,'%2d\t%2g\t',x);
fclose(fid);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by