Carmichael Numbers in Java

In this section, we will learn what is Carmichael number and also create Java programs to check if the given number is a Carmichael number or not. The Carmichael numbers program is frequently asked in Java coding interviews and academics.

Carmichael Numbers

A composite number n that are pseudoprimes to every base 'a' i.e. an-1≡ 1(mod n) for all integer 'a' with GCD(a, n)=1 are called absolute pseudoprime or Carmichael numbers.

The number must satisfy the necessary and sufficient conditions for Carmichael numbers. n is absolute pseudoprime or Carmichael number if:

• n is composite (a positive integer that can be formed by multiplying two smaller positive integers) and square-free (n is not divisible by the square of any prime).
• an-1≡1(mod n) for all integers a with GCD(a, n)=1

They are extremely rare. Note that every Carmichael number is the product of at least three distinct primes.

In other words, we can say that a composite number n is a Carmichael number if whenever a relatively prime to 'n', we have:

an-1≡1(mod n)

Let's see how to calculate Carmichael number.

Example of Carmichael Numbers

Let's check 561 is a Carmichael number or not.

If the number satisfies the above two conditions, it means 561 is a Carmichael number.

1. Consider 561=3*11*17, hence 561 is a composite number and satisfies the first condition.
2. According to the second condition, a560 ≡1(mod 561) for all integers 'a' with (a, n) = 1.

Let, GCD (a, 561) = 1

Claim: a560 ≡ 1 mod 561

a2≡ 1 mod 3
a10≡ 1 mod 11
a16≡ 1 mod 17

Suppose, 'a' that is relatively prime to 561. So, a is not a multiple of 3 or 11 or 17. Now despite the fact that 561 is not prime it still satisfies the conclusion of the formal theorem we are going to show that 'a' raised to the power of 560 is congruent to 1 modulo 561.

Now 3, 11, and 17 are prime numbers, and consequently by formosanal theorem, 'a' square is congruent to 1 modulo 3 'a' to the 10th power is congruent to 1 modulo 11, and 'a' to the 16th power is congruent to 1 modulo 17.

Now we will take the first congruence, 'a' square is congruent to 1 modulo 3 and we will raise both sides the power to conclude that 'a' to the 80th power is congruent to 1 modulo 3. We can do the same thing with the next congruence. We can take 'a' to the power 10th power is congruent to 1 modulo 11 and raise both sides to the eight power to calculate that 'a' to the 80th power is congruent to 1 modulo 11 and finally we can take that last congruence 'a' to the 16th is congruent to 1 modulo 17 and we can raise both sides to the fifth power in order to calculate that 'a' to the 80th power is congruent to 1 modulo 17.

(a2)40≡ 1 mod 3
(a10)8≡ 1 mod 11
(a16)5≡ 1 mod 17

Now, we will combine all together using the Chinese Remainder Theorem (CRT).

We know that 'a' to the 80th power is 1 modulo 3, 11, and 17. So, we can conclude that 'a' to the 80th power is 1 modulo 561. Therefore, by CRT:

a80=1 mod 561

We are going to perform one more step here is that take 'a' to the 80th congruent to 1 modulo 561. Raise both sides to the 7th power, we found that 'a' to the 560 is congruent to 1 modulo 561. Therefore,

(a80)7=a560≡ 1 mod 561

So, despite the fact that 561 is not a prime number it still satisfies the conclusion of hermozilla theorem (whenever 'a' is relatively prime to n then a to the n minus first power is congruent to one modulo n).

First few Carmichael numbers are 561, 1105, 1729, 2465, 2821, 6601, 8911, 10585, 15841.

Algorithm

2. Iterate from 2 to n and for every iteration check if gcd(b, n) = 1 and bn - 1 mod n = 1. If all the iterations satisfy the given conditions print "n is a Carmicheal number" else print "n is not a Carmicheal number".
3. Stop

Carmichael Numbers Java Program

CheckCarmichaelNumber.java

Output 1:

```Enter the Number: 10585
10585 is a Carmichael number.
```

Output 2:

```Enter the Number: 564
564 is not a Carmichael number.
```