VOOZH about

URL: https://www.geeksforgeeks.org/dsa/carmichael-numbers/

⇱ Carmichael Numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Carmichael Numbers

Last Updated : 8 May, 2023

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. 

Output: 

0
1
1

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


 

Comment
Article Tags:
Article Tags: