I am trying to convert the following function which test the number if it's prime to another one that test if the integer is a circular prime. eg. 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime. So i need to rotate the digits of the integer and test if the number is prime or not. any ideas? note: I am new to Haskell Programming
isPrime :: Integer -> Bool
isPrime 1 = False
isPrime 2 = True
isPrime n
| (length [x | x <- [2 .. n-1], n `mod` x == 0]) > 0 = False
| otherwise = True
isCircPrime :: Integer -> Bool
Referencing your question here, you can achieve what you want by adding a single new function:
This is to add an easier to understand solution from where you already were in your specific code, as I can see you were asking for that specifically. Usage:
Mind you, I have made some type-changes. I assume you want each function to be type-specific, due to their names. Full code, taken from your example:
You can improve the efficiency and elegance of your
isPrime
function easily by implementing it as:In order to rotate numbers, we can make use of two helper functions here: one to convert a number to a list of digits, and one to convert a list of digits to a number, we do this in reverse, since that is more convenient to implement, but will not matter:
Now we can make a simple function to generate, for a list of items, all rotations:
So we can use this to construct all rotated numbers:
For example for
1425
, the rotated numbers are:I leave using
isPrime
on these numbers as an exercise.