My circular_prime function only works well for two_digit input

1 visualización (últimos 30 días)
Hi, I wrote a function to compute the number of circular primes smaller than an input. For example, 197 is a circular prime, because 197, 971, and 719 are all primes. Here is my function. It works only well for inputs less than 100. As long as I increase the input to something like 101, something will be missing. Could you help me figure it out? Thanks!
A numerical example would be
If an input is 17, then its output is m=[2 3 5 7 11 13] and x=6.
% function x=circular_primes(n)
p=primes(n-1);
l=length(p);
for i = 1:l
for j=1:numel(num2str(p(i)))
q=num2str(p(i));
if j~=1 && j<numel(num2str(p(i)))
ca=[q(j+1:end) q(1:j)];
c=str2num(ca);
elseif j==1
c=str2num(q);
elseif j==numel(num2str(p(i)))
ca=flip(num2str(q));
c=str2num(ca);
end
if isprime(c)==1
m(i)=p(i);
else
m(i)=0;
end
end
end
m(m==0)=[]
[~,x]=size(m);

Respuesta aceptada

Kelly Kearney
Kelly Kearney el 11 de Jul. de 2018
I'd suggest looking at the circshift function.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by