forked from mrizky-kur/Redux-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circular prime.java
133 lines (57 loc) · 1.44 KB
/
Circular prime.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Program to check if a number is circular
// prime or not.
#include <iostream>
#include <cmath>
using namespace std;
// Function to check if a number is prime or not.
bool isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
// Function to check if the number is circular
// prime or not.
bool checkCircular(int N)
{
// Count digits.
int count = 0, temp = N;
while (temp) {
count++;
temp /= 10;
}
int num = N;
while (isPrime(num)) {
// Following three lines generate the next
// circular permutation of a number. We move
// last digit to first position.
int rem = num % 10;
int div = num / 10;
num = (pow(10, count - 1)) * rem + div;
// If all the permutations are checked and
// we obtain original number exit from loop.
if (num == N)
return true;
}
return false;
}
// Driver Program
int main()
{
int N = 1193;
if (checkCircular(N))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}