Skip to content

Commit 4dac2ac

Browse files
committed
So close...
1 parent bd9c2a3 commit 4dac2ac

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package Challenges_VII.Carmichael_Numbers;
2+
3+
import java.math.BigInteger;
4+
import java.util.HashMap;
5+
import java.util.Scanner;
6+
7+
public class Main {
8+
9+
public static final int MAX = 65001;
10+
public static HashMap<String,Integer> primeHash;
11+
12+
public static void main(String[] args) {
13+
Scanner s = new Scanner(System.in);
14+
primeHash = new HashMap<String, Integer>();
15+
int next;
16+
genPrimes();
17+
18+
while((next = s.nextInt()) != 0){
19+
BigInteger randNum = BigInteger.valueOf((int) (Math.random() * (next - 1)) + 2);
20+
BigInteger fermatTest = randNum.pow(next).mod(BigInteger.valueOf(next));
21+
if(fermatTest.equals(randNum) && primeHash.get(String.valueOf(next)) == null){
22+
System.out.println("The number " + next + " is a Carmichael number.");
23+
}else{
24+
System.out.println(next + " is normal.");
25+
}
26+
}
27+
s.close();
28+
System.exit(0);
29+
30+
}
31+
32+
33+
public static void genPrimes(){
34+
35+
boolean[] sieve = new boolean[MAX+1];
36+
sieve[0] = true;
37+
sieve[1] = true;
38+
sieve[2] = false;
39+
40+
for(int i = 4; i <= MAX; i+=2)
41+
sieve[i] = true;
42+
43+
for(int i = 3; i < (int)Math.sqrt(MAX)+1; i+=2)
44+
{
45+
if(!sieve[i])
46+
{
47+
for(int j = i*i; j <= MAX; j+=i)
48+
sieve[j] = true;
49+
}
50+
}
51+
52+
for(int b = 0; b < sieve.length; b++){
53+
if(!sieve[b]){
54+
primeHash.put(String.valueOf(b), b);
55+
}
56+
}
57+
58+
}
59+
60+
}

0 commit comments

Comments
 (0)