Algorithm and Flowchart to check whether a number is twisted prime or not

[1905 views]




Prime Numbers:

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.

What are Twisted Prime Numbers?:

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.

CheckPrime(num):

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.

Step 1: Start Step 2: Initialize number of factors of num, f = 0 Step 3. Initialize i = 1 Step 4. Repeat until i<=num: 4.1: If num % i == 0: 4.2: Increment f by 1 4.3: Increment i by 1 Step 5: If f = 2, then: 5.1: Return True Step 6: Else: 6.1 Return False Step 7. Stop

Algorithm to check whether a number is twisted prime or not:

Step 1: Start Step 2: Read the number to be checked from the user, say n Step 3: original = CheckPrime(n) Step 4: Initialize rev = 0 Step 5. Repeat WHILE n ? 0: 5.1: Extract the last digit by: d = n % 10 5.2: Calculate reverse by: rev = (rev*10) + d 5.3: Remove the last digit from the number: n = n / 10 Step 6: reverse = CheckPrime(rev) Step 7: If original = True AND reverse = True, then: 7.1: Display “Given number is twisted prime” Step 8: Else: 8.1: Display “Given number is not twisted prime” Step 9: Stop

Explanation:

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.

Flowchart to check whether a number is twisted prime or not:

Algorithm and Flowchart to check whether a number is twisted prime or not
                 



Having Difficulty understanding above Algorithm and Flowchart? Buy my Personal Notes



Are You Good enough in Algorithms? Prove it!



Comments





Search
Get Answers to your Programming Questions


Recommended Deals ends in








Test Your Skills:

Search Tags

    Algorithm to Find if a number is Twisted Prime

    Flowchart to find if number is Twisted Prime

    Twisted Prime or Not Pseudocode