Javatpoint Logo
Javatpoint Logo

Circular Primes in Java

Prime numbers have always fascinated mathematicians due to their unique properties and applications in various fields. One such intriguing aspect of prime numbers is circular primes, which are primes that remain prime when their digits are rotated cyclically. In this article, we will delve into circular primes and demonstrate how to identify them using Java.

Understanding Circular Primes:

A circular prime is a prime number that remains prime after rotating its digits in a circular manner.

Circular Primes Example

consider the prime number 197. When we rotate its digits, we get 971 or 719, both of which are also prime numbers. Hence, 197 a circular prime.

To determine if a number is a circular prime, we need to check if all possible rotations of its digits are prime. If any rotation results in a non-prime number, the original number is not a circular prime.

How to Check if the given number is Circular Prime or not in Java?

Let's now explore how to identify circular primes in Java using a simple algorithm. Here are the steps:

Step 1: Implement a Function to Check Prime Numbers:

To determine if a number is prime, we need a helper function. We can create a method that takes an integer as input and checks if it is divisible by any number between 2 and the square root of the input number (inclusive). If the number is divisible by any of these numbers, it is not prime. Otherwise, it is prime. Here's an example of such a function:

Step 2: Implement a Function to Check Circular Primes:

Next, we can create a method to check if a given number is a circular prime. This function will iterate through all possible rotations of the number's digits and verify if each rotation is a prime number. If any rotation is not prime, the original number is not a circular prime. Here's an example of such a function:

Step 3: Generating Circular Primes within a Range:

To find all circular primes within a given range, we can iterate through the numbers in the range and check if each number is a circular prime using the isCircularPrime method. If it is, we can store it or perform further operations as desired. Here's an example of generating circular primes within a range:

Usage Example:

Now, let's put everything together and see how to use these methods to find circular primes within a given range. Here's an example:

The code generates and prints all circular primes between 1 and 1000.

Here's the complete code along with the output:

File Name: CircularPrimesExample.java

Output:

Circular Primes within the range 1 to 1000:
2
3
5
7
11
13
17
31
37
71
73
79
97
113
131
197
199
311
337
373
719
733
919
971
991

Above code generates and prints all circular primes between 1 and 1000. The circular primes within the given range are displayed in ascending order. In this case, the circular primes range from 2 to 991.

Circular primes possess an interesting property where all possible rotations of their digits remain prime numbers. By implementing the provided methods to check for prime numbers and circular primes, you can easily identify and explore circular primes within different ranges.

Conclusion:

Circular primes offer an interesting twist to the concept of prime numbers. Through rotating the digits of a prime number and verifying if each rotation remains prime, we can identify circular primes. In this article, we explored the concept of circular primes and demonstrated how to detect them using Java. By implementing the functions to check prime numbers and circular primes, you can easily identify circular primes within a given range. By further exploring and studying these unique numbers, we can deepen your understanding of prime numbers and their fascinating properties.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA