Write a function called circular_primes that finds the number of circular prime numbers smaller than n, where n is a positive integer scalar input argument. For example, the number, 197, is a circular prime because all rotations of its digits:
3 次查看(过去 30 天)
显示 更早的评论
Hi,Please give me hints to develop this code.
0 个评论
回答(1 个)
RAMAKANT SHAKYA
2019-2-7
function out=circular_primes(no)
prim=primes(no);% find the all prime number till the given number
pr=0;
nos=[];
po=[];
for p=1:length(prim)
n=prim(p); % picking up each prime no one by one
n=num2str(n);% change into string for rotation of no
d=length(n); % length of string
if d>1 % take nos greater than 10 beacuase below 10 no need for rotation
for h=1:d
a=n(1);
for r=1:d % for rotation right to left
if r==d 5 % for the last element of string
n(d)=a;
else
n(r)=n(r+1); %shifting
end
end
s=str2num(n); % string to number
nos=[nos,s]; % store rotated elements in array
end
if nos(end)==no %if given no is also a circular prime we need smaller
break;
end
for gr=1:length(nos) % checking rotated nos are prime or not
p1=isprime(nos(gr));
po=[po,p1]; %storing logical result in array
end
if sum(po(:))==length(nos) %if all are prime the length and sum are must be equal
pr=pr+1;
out=pr;
else
out=pr;
end
po=[];
nos=[];
else
s=str2num(n); %numbers less than 10
f=isprime(s);
if f==1
pr=pr+1;
out=pr;
else
out=pr;
end
end
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Assembly 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!