-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion035.java
59 lines (56 loc) · 1.17 KB
/
Question035.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
package euler;
public class Question035
{
public static void main(String[] args)
{
int max = 1000000;
int circularPrimeCount = 1;
for (int tempI = 3; tempI < max; tempI += 2)
{
int i = tempI;
int digits = getDigits(i);
int digitSpinCount = 0;
boolean circularPrime = true;
while (digitSpinCount < digits)
{
if (!testPrime(i))
{
circularPrime = false;
break;
}
int endingDigits = (int) (i % Math.pow(10, digits - 1));
int startingDigit = (int) (i / Math.pow(10, digits - 1));
i = endingDigits * 10 + startingDigit;
digitSpinCount++;
}
if (circularPrime)
{
System.out.println(tempI);
circularPrimeCount++;
}
}
System.out.printf("Total number of circular prime numbers below %d: %d\n", max, circularPrimeCount);
System.out.println(new java.util.Date());
}
private static int getDigits(int n)
{
int dig = 0;
while (n > 0)
{
n /= 10;
dig++;
}
return dig;
}
private static boolean testPrime(long i)
{
for (int j = 2; j < (int) Math.sqrt(i) + 1; j++)
{
if (i % j == 0)
{
return false;
}
}
return true;
}
}