MATLAB Answers

0

My circular_prime function only works well for two_digit input

Asked by Qingchuan Lyu on 11 Jul 2018
Latest activity Answered by Kelly Kearney
on 11 Jul 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);

  0 Comments

Sign in to comment.

1 Answer

Answer by Kelly Kearney
on 11 Jul 2018
 Accepted Answer

I'd suggest looking at the circshift function.

  0 Comments

Sign in to comment.