In this article, I will explain how to determine whether a given number is a twisted prime number or not, and provide a comprehensive guide to do so, covering essential concepts, methods, and examples.
- Introduction
- What is a Prime Number?
- What is a Twisted Prime Number?
- How to check whether a given number is a twisted prime number or not in C#?
- Algorithm to check twisted prime numbers
- Program for Twisted Prime Number in C#
- Twisted Prime number between a range of numbers
- Conclusion
What is a Prime Number?
As per Wikipedia, "A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself".
In simple words, "A prime number is one which is divisible only by one and itself and is greater than one" For example, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 47, 53,…., are prime numbers.
What is a Twisted Prime Number?
A number is said to be a twisted prime number if it is a prime number and the reverse of the number is also a prime number.
For example, 11, 13, 17, 31, 37, 71, 73, 79, 97,…., are twisted prime numbers.
Algorithm to check Twisted Prime Number
Below are the steps to check whether a given number is a twisted prime number or not.
- Get the input from the user that you want to check if it is a twisted prime number.
- Check if a number is a prime number or not.
- Reverse the number and check if the reversed number is also prime.
- If both the number and reverse of a number are prime, then it is a twisted prime number.
Program for Twisted Prime Number in C#
The following C# program allows the user to input a number and check whether that number is a twisted prime number. A twisted prime number is defined as a number that is prime and its reverse is also a prime number. The program performs these checks to determine whether the given number qualifies as a twisted prime number.
internal class TwistedPrimeNumberInCSharp
{
public static void Main()
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int revNumber = reverseNum(number);
if (IsPrime(number) && IsPrime(revNumber))
{
Console.WriteLine($"Yesss, {number} is a Twisted Prime Number!");
}
else
{
Console.WriteLine($"Oohhh, {number} is not a Twisted Prime Number!");
}
Console.ReadLine();
}
// Function to reverse the digits of a number
public static int reverseNum(int num)
{
int revNo = 0;
while (num > 0)
{
revNo = revNo * 10 + (num % 10);
num = num / 10;
}
return revNo;
}
// Function to check if a number is prime
public static bool IsPrime(int num)
{
if (num <= 1) return false; // Numbers less than or equal to 1 are not prime
if (num == 2) return true; // 2 is a prime number
if (num % 2 == 0) return false; // Even numbers are not prime except 2
for (int i = 3; i < num / 2; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
}
Sample Output
- Input: 13
- Output: Twisted Prime Number
- Explanation: 13 is a prime number and its reverse 31 is also a prime number.
- Input: 23
- Output: Not a Twisted Prime Number
- Explanation: 23 is a prime number but its reverse 32 is not a prime number.
Twisted Prime number between a range of numbers
In this program, we take two inputs from the user: a starting number and an ending number. Using these inputs, the program identifies all Twisted Prime Numbers within the specified range.
internal class TwistedPrimeNumberBetweenRangeOfNumbers
{
public static void Main()
{
Console.Write("Enter the Start Number: ");
int StartNumber = int.Parse(Console.ReadLine());
Console.Write("Enter the End Number: ");
int EndNumber = int.Parse(Console.ReadLine());
Console.WriteLine($"Twisted Prime Numbers between {StartNumber} and {EndNumber} are: ");
for (int number = StartNumber; number <= EndNumber; number++)
{
int revNumber = ReverseNo(number);
if (IsPrime(number) && IsPrime(revNumber))
Console.WriteLine(number);
}
Console.ReadLine();
}
// Function to reverse the digits of a number
public static int ReverseNo(int num)
{
int revNo = 0;
while (num > 0)
{
revNo = revNo * 10 + (num % 10);
num = num / 10;
}
return revNo;
}
// Function to check if a number is prime
public static bool IsPrime(int num)
{
if (num <= 1) return false;
if (num == 2) return true; // 2 is a prime number
if (num % 2 == 0) return false; // Even numbers are not prime except 2
for (int i = 3; i < num / 2; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
}
Output
See you in the next article, till then, take care and be happy learning.
You may also visit my other article,
You can connect with me @
Conclusion
In this article, we have discussed how to determine whether a given number is a twisted prime number or not, and provided a comprehensive guide to doing so, covering essential concepts and examples.
I hope you enjoyed this blog. Follow C# Corner to learn more new and amazing things about C#.
Thanks for reading!