My circular_prime function only works well for two_digit input
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!