The Wayback Machine - https://web.archive.org/web/20240627104139/https://www.geeksforgeeks.org/carmichael-numbers/
Open In App

Carmichael Numbers

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A number n is said to be a Carmichael number if it satisfies the following modular arithmetic condition: 
 

  power(b, n-1) MOD n = 1, 
  for all b ranging from 1 to n such that b and 
  n are relatively prime, i.e, gcd(b, n) = 1 

Given a positive integer n, find if it is a Carmichael number. These numbers have importance in Fermat Method for primality testing.
Examples : 
 

Input :  n = 8
Output : false
Explanation : 8 is not a Carmichael number because 3 is 
              relatively prime to 8 and (38-1) % 8
              = 2187 % 8 is not 1.
              
Input :  n = 561
Output : true
Recommended Practice

The idea is simple, we iterate through all numbers from 1 to n and for every relatively prime number, we check if its (n-1)th power under modulo n is 1 or not. 
Below is a the program to check if a given number is Carmichael or not. 

C++




// A C++ program to check if a number is
// Carmichael or not.
#include <iostream>
using namespace std;
 
// utility function to find gcd 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);
}
 
// utility function to find pow(x, y) under
// given modulo mod
int power(int x, int y, int mod)
{
    if (y == 0)
        return 1;
    int temp = power(x, y / 2, mod) % mod;
    temp = (temp * temp) % mod;
    if (y % 2 == 1)
        temp = (temp * x) % mod;
    return temp;
}
 
// This function receives an integer n and
// finds if it's a Carmichael number
bool isCarmichaelNumber(int n)
{
    for (int b = 2; b < n; b++) {
        // If "b" is relatively prime to n
        if (gcd(b, n) == 1)
 
            // And pow(b, n-1)%n is not 1,
            // return false.
            if (power(b, n - 1, n) != 1)
                return false;
    }
    return true;
}
 
// Driver function
int main()
{
    cout << isCarmichaelNumber(500) << endl;
    cout << isCarmichaelNumber(561) << endl;
    cout << isCarmichaelNumber(1105) << endl;
    return 0;
}


Java




// JAVA program to check if a number is
// Carmichael or not.
import java.io.*;
 
class GFG {
 
    // utility function to find gcd of
    // two numbers
    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);
    }
 
    // utility function to find pow(x, y)
    // under given modulo mod
    static int power(int x, int y, int mod)
    {
        if (y == 0)
            return 1;
        int temp = power(x, y / 2, mod) % mod;
        temp = (temp * temp) % mod;
        if (y % 2 == 1)
            temp = (temp * x) % mod;
        return temp;
    }
 
    // This function receives an integer n and
    // finds if it's a Carmichael number
    static int isCarmichaelNumber(int n)
    {
        for (int b = 2; b < n; b++) {
            // If "b" is relatively prime to n
            if (gcd(b, n) == 1)
 
                // And pow(b, n-1)%n is not 1,
                // return false.
                if (power(b, n - 1, n) != 1)
                    return 0;
        }
        return 1;
    }
 
    // Driver function
    public static void main(String args[])
    {
        System.out.println(isCarmichaelNumber(500));
        System.out.println(isCarmichaelNumber(561));
        System.out.println(isCarmichaelNumber(1105));
    }
}
// This code is contributed by Nikita Tiwari.


Python3




# A Python program to check if a number is
# Carmichael or not.
 
# utility function to find gcd of two numbers
def gcd( a, b) :
    if (a < b) :
        return gcd(b, a)
    if (a % b == 0) :
        return b
    return gcd(b, a % b)
 
# utility function to find pow(x, y) under
# given modulo mod
def power(x, y, mod) :
    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
 
 
# This function receives an integer n and
# finds if it's a Carmichael number
def isCarmichaelNumber( n) :
    b = 2
    while b<n :
         
        # If "b" is relatively prime to n
        if (gcd(b, n) == 1) :
 
            # And pow(b, n-1)% n is not 1,
            # return false.
            if (power(b, n - 1, n) != 1):
                return 0
        b = b + 1
    return 1
 
# Driver function
print (isCarmichaelNumber(500))
print (isCarmichaelNumber(561))
print (isCarmichaelNumber(1105))
 
# This code is contributed by Nikita Tiwari.


C#




// C# program to check if a number is
// Carmichael or not.
using System;
 
class GFG {
 
    // utility function to find gcd of
    // two numbers
    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);
    }
 
    // utility function to find pow(x, y)
    // under given modulo mod
    static int power(int x, int y, int mod)
    {
        if (y == 0)
            return 1;
 
        int temp = power(x, y / 2, mod) % mod;
        temp = (temp * temp) % mod;
 
        if (y % 2 == 1)
            temp = (temp * x) % mod;
 
        return temp;
    }
 
    // This function receives an integer n and
    // finds if it's a Carmichael number
    static int isCarmichaelNumber(int n)
    {
        for (int b = 2; b < n; b++) {
            // If "b" is relatively prime to n
            if (gcd(b, n) == 1)
 
                // And pow(b, n-1)%n is not 1,
                // return false.
                if (power(b, n - 1, n) != 1)
                    return 0;
        }
        return 1;
    }
 
    // Driver function
    public static void Main()
    {
        Console.WriteLine(isCarmichaelNumber(500));
        Console.WriteLine(isCarmichaelNumber(561));
        Console.WriteLine(isCarmichaelNumber(1105));
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP program to check if a
// number is Carmichael or not.
 
// utility function to find
// gcd of two numbers
function gcd($a, $b)
{
    if ($a < $b)
        return gcd($b, $a);
    if ($a % $b == 0)
        return $b;
    return gcd($b, $a % $b);
}
 
// utility function to find
// pow(x, y) under given modulo mod
function power($x, $y, $mod)
{
    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;
}
 
// This function receives an integer
// n and finds if it's a Carmichael
// number
function isCarmichaelNumber($n)
{
    for ($b = 2; $b <= $n; $b++)
    {
        // If "b" is relatively
        // prime to n
        if (gcd($b, $n) == 1)
 
            // And pow(b, n - 1) % n
            // is not 1, return false.
            if (power($b, $n - 1, $n) != 1)
                return 0;
    }
    return 1;
}
 
// Driver Code
echo isCarmichaelNumber(500), " \n";
echo isCarmichaelNumber(561), "\n";
echo isCarmichaelNumber(1105), "\n";
 
// This code is contributed by ajit
?>


Javascript




<script>
 
    // Javascript program to check if a number is
    // Carmichael or not.
     
    // utility function to find gcd of
    // two numbers
    function gcd(a, b)
    {
        if (a < b)
            return gcd(b, a);
        if (a % b == 0)
            return b;
        return gcd(b, a % b);
    }
   
    // utility function to find pow(x, y)
    // under given modulo mod
    function power(x, y, mod)
    {
        if (y == 0)
            return 1;
   
        let temp = power(x, parseInt(y / 2, 10), mod) % mod;
        temp = (temp * temp) % mod;
   
        if (y % 2 == 1)
            temp = (temp * x) % mod;
   
        return temp;
    }
   
    // This function receives an integer n and
    // finds if it's a Carmichael number
    function isCarmichaelNumber(n)
    {
        for (let b = 2; b < n; b++) {
            // If "b" is relatively prime to n
            if (gcd(b, n) == 1)
   
                // And pow(b, n-1)%n is not 1,
                // return false.
                if (power(b, n - 1, n) != 1)
                    return 0;
        }
        return 1;
    }
     
    document.write(isCarmichaelNumber(500) + "</br>");
    document.write(isCarmichaelNumber(561) + "</br>");
    document.write(isCarmichaelNumber(1105));
 
</script>


C




// C Program to find if a number is Carmichael Number
#include<stdio.h>
int gcd(int a, int b)    //Function to find GCD
{
if (a<b)
return gcd(b, a);
if (a % b == 0)
return b;
return gcd(b, a % b);
}
 
// Function to find pow(x,y) under given modulo mod
int power(int x, int y, int mod)   
{
if (y == 0)
return 1;
int temp = power(x, y / 2, mod) % mod;
temp = (temp * temp) % mod;
if (y % 2 == 1)
temp = (temp * x) % mod;
return temp;
}
 
 
//Function to find if received number n is a Carmichael number
int carmichaelnumber(int n)   
{
for (int b=2;b<n;b++)
{
if (gcd(b,n)==1)
if (power(b,n-1,n)!= 1)
{
printf("0");
return 0;
}
}
printf("1");
return 0;
};
int main()
{
carmichaelnumber(500);
printf("\n");
carmichaelnumber(561);
printf("\n");
carmichaelnumber(1105);
return 0;
   
// This code is contributed by Susobhan Akhuli
 
}


Output: 

0
1
1

Time Complexity: O(n log n)
Auxiliary Space: O(n)

 



Previous Article
Next Article

Similar Reads

Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
Given an integer N, the task is to print N integers ? 109 such that no two consecutive of these integers are co-prime and every 3 consecutive are co-prime. Examples: Input: N = 3 Output: 6 15 10Input: N = 4 Output: 6 15 35 14 Approach: We can just multiply consecutive primes and for the last number just multiply the gcd(last, last-1) * 2. We do thi
15+ min read
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order
Given two integers N and K, the task is to find the Kth element in the permutation of first N natural numbers arranged such that all the even numbers appear before the odd numbers in increasing order. Examples : Input: N = 10, K = 3 Output: 6Explanation:The required permutation is {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}.The 3rd number in the permutation is
9 min read
Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices
Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices. Example: Input : N = 1Output: 5Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers. Input: N = 2Output: 20 Explanation: There are 20
5 min read
Permutation of numbers such that sum of two consecutive numbers is a perfect square
Prerequisite: Hamiltonian Cycle Given an integer n(&gt;=2), find a permutation of numbers from 1 to n such that the sum of two consecutive numbers of that permutation is a perfect square. If that kind of permutation is not possible to print "No Solution". Examples: Input : 17 Output : [16, 9, 7, 2, 14, 11, 5, 4, 12, 13, 3, 6, 10, 15, 1, 8, 17] Expl
15+ min read
Numbers within a range that can be expressed as power of two numbers
Given two integers L and R. Find the number of perfect powers in the given range [L, R]. A number x is said to be perfect power if there exists some integers a &gt; 0, p &gt; 1 such that x = ap. Examples : Input : 1 4 Output : 2 Explanation : Suitable numbers are 1 and 4 where 1 can be expressed as 1 = 12 and 4 can be expressed as 4 = 22 Input : 12
12 min read
8085 program to count total even numbers in series of 10 numbers
Program - Write an assembly language program in 8085 microprocessor to count even numbers in a series of 10 numbers. Example - Assumption - Ten 8-bit numbers are stored from starting memory location 2050. Value of count is stored at memory location 3050. Algorithm - Initialize register H with 20 and register L with 4F so that indirect memory points
4 min read
8085 program to count total odd numbers in series of 10 numbers
Program - Write an assembly language program in 8085 microprocessor to count odd numbers in series of 10 numbers. Example - Assumption - Ten 8-bit numbers are stored from starting memory location 2050. Value of count is stored at memory location 3050. Algorithm - Initialize register H with 20 and register L with 4F so that indirect memory points to
3 min read
Print all distinct integers that can be formed by K numbers from a given array of N numbers
Given an array of N elements and an integer K, print all the distinct integers which can be formed by choosing K numbers from the given N numbers. A number from an array can be chosen any number of times. Examples: Input: k = 2, a[] = {3, 8, 17, 5} Output: The 10 distinct integers are: 6 8 10 11 13 16 20 22 25 34 The 2 elements chosen are: 3+3 = 6,
7 min read
Numbers less than N which are product of exactly two distinct prime numbers
Given a number [Tex]N [/Tex]. The task is to find all such numbers less than N and are a product of exactly two distinct prime numbers. For Example, 33 is the product of two distinct primes i.e 11 * 3, whereas numbers like 60 which has three distinct prime factors i.e 2 * 2 * 3 * 5.Note: These numbers cannot be a perfect square.Examples: Input : N
8 min read
Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
Given N and K, the task is to print N lines where each line contains 4 numbers such that every among those 4 numbers has a GCD K and the maximum number used in N*4 should be minimized.Note: In case of multiple outputs, print any one. Examples: Input: N = 1, K = 1 Output: 1 2 3 5 Every pair among 1, 2, 3 and 5 gives a GCD K and the largest number am
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg