Carmichael Numbers - GeeksforGeeks

22 Jan, 2022
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
 
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++
Java
Python3
C#
PHP
Javascript
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 numbersint 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 modint 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 numberbool 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 functionint main(){    cout << isCarmichaelNumber(500) << endl;    cout << isCarmichaelNumber(561) << endl;    cout << isCarmichaelNumber(1105) << endl;    return 0;}
// 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.
# A Python program to check if a number is# Carmichael or not. # utility function to find gcd of two numbersdef 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 moddef 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 numberdef 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 functionprint (isCarmichaelNumber(500))print (isCarmichaelNumber(561))print (isCarmichaelNumber(1105)) # This code is contributed by Nikita Tiwari.
// 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 program to check if a// number is Carmichael or not. // utility function to find// gcd of two numbersfunction 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 modfunction 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// numberfunction 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 Codeecho isCarmichaelNumber(500), " \n";echo isCarmichaelNumber(561), "\n";echo isCarmichaelNumber(1105), "\n"; // This code is contributed by ajit?>
<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 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 modint 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 numberint 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
This article is contributed by Ashutosh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
divyeshrabadiya07
susobhanakhuli
amartyaghoshgfg
number-theory
series
Mathematical
number-theory
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Modular multiplicative inverse
Program to multiply two matrices
Program to convert a given number to words
Count ways to reach the n'th stair
Fizz Buzz Implementation
Singular Value Decomposition (SVD)
Program to print prime numbers from 1 to N.
Find first and last digits of a number
Check if a number is Palindrome