[2864 views]
A number is said to be a prime number when it has only two factors, that is, when the factors of the number are 1 and the number itself. Example: 2, 3, 11, 17, etc.
A prime number is said to be ‘twisted prime’ when on reversing the number, the new number is also a prime number. Twisted prime numbers are also known as Emirp numbers.
For example: 97 and 79.
97 is a prime number. On reversing 97, we get 79, which is also a prime number. Therefore, both 97 and 79 are twisted prime numbers.
Other examples include: 2, 3, 5, 7, 13, 17, 37, etc.
Now let us have a look at the algorithm and flowchart to check whether a given number is twisted prime or not.
To avoid redundancy, we will use the concept of functions. Here, we will use a function to check whether a number is prime or not. While writing the program, we can call this function as many times we want.
Assumption: For this algorithm, we assume that the function takes a number ‘num’, to be checked and returns true if the number is prime, otherwise, returns false.
We start this algorithm by taking the number to be checked as input from the user. To check whether the number is twisted prime, we first need to check whether the number is prime, and then reverse the number.
To check whether the given number is prime, we count the number of factors of that number. We start a loop which runs from 1 to num. If the number is divisible by the loop variable, then the loop variable is a factor of that number. Therefore, we increment the number of factors by one. Once this check is done, we now have to reverse the number.
To reverse the number, we initialize a variable, say rev as 0. This variable will store the reverse. We now start a loop which will run until the number is not equal to zero. We extract the last digit of the number, by doing: d = n % 10. This last digit is added to (rev * 10); to maintain the place values. Now, the last digit is removed from the number by doing: n = n /10. Once this loop completes its execution, we get the reverse in rev.
Now, we will check whether the reversed number is prime or not, using the same method as before. If both the original and reversed numbers are prime, the given number is twisted prime, else, it is not twisted prime.
Let us consider an example:
n = 71
71 is a prime number.
Reverse of the number, rev = 17
17 is also a prime number.
Therefore, 71 is twisted prime.