Circular Primes

Matlab. This was one of the assignments in the Matlab course I took. I developed an algorithm to find total number of circular primes under a positive integer as input.

Question: 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: 197, 971, and 719, are themselves prime. For instance, there are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. It is important to emphasize that rotation means circular permutation not all possible permutations.

Solution: My approach to solve it was divide and conquer approach. Firstly, I checked one digit prime numbers. Then, I deleted all primes whose digits include 0,2,4,5,6,8 because those integers would make the prime divisible by either 2 or 5 when rotated. I called it “simplification stage”, and nested loop algorithm was faster than built-in “strfind” function. While checking permutations of each prime left to see if they are circular primes or not, non-circular primes were indexed as zero. At its final stage, all of zeros was deleted, and all circular primes were left.