My circular_prime function only works well for two_digit input
    9 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    N/A
 le 11 Juil 2018
  
    
    
    
    
    Réponse apportée : Kelly Kearney
      
 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);
0 commentaires
Réponse acceptée
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Loops and Conditional Statements 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!
