My circular_prime function only works well for two_digit input

1 vue (au cours des 30 derniers jours)
Qingchuan Lyu
Qingchuan Lyu le 11 Juil 2018
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);

Réponse acceptée

Kelly Kearney
Kelly Kearney le 11 Juil 2018
I'd suggest looking at the circshift function.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by