A positive integer n is called a Carmichael number if, for every integer b such that 1 âĪ b < n and gcd(b, n) = 1, the following condition holds: bn-1 mod n = 1.
Given a positive integer n, determine whether it is a Carmichael number.
Examples :
Input: n = 8
Output: false
Explanation: 3 is relatively prime to 8 but 3(8 - 1) % 8 = 2187 % 8 != 1Input: n = 3
Output: true
Explanation: For both 1 and 2 the condition satisfied.
Try It Yourself
The idea is to check the Carmichael condition for every number that is coprime with n while computing i^(n-1) mod n efficiently using binary exponentiation. This reduces the exponentiation time from linear to logarithmic, making the overall solution much faster.
Working of Approach:
- Traverse every integer from 2 to n-1.
- Skip numbers that are not coprime with n.
- Compute i^(n-1) mod n using binary exponentiation.
- If the result is not 1, return false.
- If every coprime number satisfies the condition, return true.
Let us understand with an example:
- For n = 3, the loop runs for i = 2.
- gcd(2, 3) = 1, so 2 is coprime with 3.
- Compute 2^(3-1) mod 3 = 2Âē mod 3 = 4 mod 3 = 1.
- Since the result is 1, the Carmichael condition is satisfied for i = 2.
- No coprime number violates the condition, so the function returns true.
#include <iostream>
using namespace std;
// Function to calculate the greatest common divisor of two numbers.
int gcd(int a, int b)
{
if (a < b)
return gcd(b, a);
if (a % b == 0)
return b;
return gcd(b, a % b);
}
// Function to calculate the modular exponentiation using recursive approach.
long long power(int x, int y, int mod)
{
if (y == 0)
return 1;
long long temp = power(x, y / 2, mod) % mod;
temp = (temp * temp) % mod;
if (y % 2 == 1)
temp = (temp * x) % mod;
return temp;
}
// Function to check if a given number is a Carmichael number.
bool isCarmichael(int n)
{
for (int i = 2; i < n; i++)
{
// Checking if i and n are coprime
if (gcd(i, n) == 1)
// Checking if i^(n-1) is congruent to 1 modulo n
if (power(i, n - 1, n) != 1)
return false;
}
return true;
}
int main()
{
int n = 3;
if (isCarmichael(n))
cout << "true";
else
cout << "false";
return 0;
}
import java.util.*;
public class GFG {
// Function to calculate the greatest common divisor of
// two numbers.
public static int gcd(int a, int b)
{
if (a < b)
return gcd(b, a);
if (a % b == 0)
return b;
return gcd(b, a % b);
}
// Function to calculate the modular exponentiation
// using recursive approach.
public static long power(int x, int y, int mod)
{
if (y == 0)
return 1;
long temp = power(x, y / 2, mod) % mod;
temp = (temp * temp) % mod;
if (y % 2 == 1)
temp = (temp * x) % mod;
return temp;
}
// Function to check if a given number is a Carmichael
// number.
public static boolean isCarmichael(int n)
{
for (int i = 2; i < n; i++) {
// Checking if i and n are coprime
if (gcd(i, n) == 1)
// Checking if i^(n-1) is congruent to 1
// modulo n
if (power(i, n - 1, n) != 1)
return false;
}
return true;
}
public static void main(String[] args)
{
int n = 3;
if (isCarmichael(n))
System.out.println("true");
else
System.out.println("false");
}
}
def gcd(a, b):
# Function to calculate the greatest common divisor of two numbers.
if a < b:
return gcd(b, a)
if a % b == 0:
return b
return gcd(b, a % b)
def power(x, y, mod):
# Function to calculate the modular exponentiation using recursive approach.
if y == 0:
return 1
temp = power(x, y // 2, mod) % mod
temp = (temp * temp) % mod
if y % 2 == 1:
temp = (temp * x) % mod
return temp
def isCarmichael(n):
# Function to check if a given number is a Carmichael number.
for i in range(2, n):
# Checking if i and n are coprime
if gcd(i, n) == 1:
# Checking if i^(n-1) is congruent to 1 modulo n
if power(i, n - 1, n) != 1:
return False
return True
if __name__ == "__main__":
n = 3
if isCarmichael(n):
print("true")
else:
print("false")
using System;
public class GFG {
// Function to calculate the greatest common divisor of
// two numbers.
public static int gcd(int a, int b)
{
if (a < b)
return gcd(b, a);
if (a % b == 0)
return b;
return gcd(b, a % b);
}
// Function to calculate the modular exponentiation
// using recursive approach.
public static long power(int x, int y, int mod)
{
if (y == 0)
return 1;
long temp = power(x, y / 2, mod) % mod;
temp = (temp * temp) % mod;
if (y % 2 == 1)
temp = (temp * x) % mod;
return temp;
}
// Function to check if a given number is a Carmichael
// number.
public static bool isCarmichael(int n)
{
for (int i = 2; i < n; i++) {
// Checking if i and n are coprime
if (gcd(i, n) == 1)
// Checking if i^(n-1) is congruent to 1
// modulo n
if (power(i, n - 1, n) != 1)
return false;
}
return true;
}
public static void Main()
{
int n = 3;
if (isCarmichael(n))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
function gcd(a, b)
{
// Function to calculate the greatest common divisor of
// two numbers.
if (a < b)
return gcd(b, a);
if (a % b == 0)
return b;
return gcd(b, a % b);
}
function power(x, y, mod)
{
// Function to calculate the modular exponentiation
// using recursive approach.
if (y == 0)
return 1;
let temp = power(x, Math.floor(y / 2), mod) % mod;
temp = (temp * temp) % mod;
if (y % 2 == 1)
temp = (temp * x) % mod;
return temp;
}
function isCarmichael(n)
{
// Function to check if a given number is a Carmichael
// number.
for (let i = 2; i < n; i++) {
// Checking if i and n are coprime
if (gcd(i, n) == 1)
// Checking if i^(n-1) is congruent to 1 modulo
// n
if (power(i, n - 1, n) != 1)
return false;
}
return true;
}
// Driver Code
let n = 3;
if (isCarmichael(n))
console.log("true");
else
console.log("false");
Output
true