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.
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.
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 contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Numbers within a range that can be expressed as power of two numbers
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Ugly Numbers
- Program for Fibonacci numbers
- Euler's Totient function for all numbers smaller than or equal to n
- Print all Good numbers in given range
- Lucas Numbers
- Emirp numbers
- Equidigital Numbers
- Program for Perrin numbers
- Sum of Perrin Numbers
- Reversible numbers
- Number of occurrences of 2 as a digit in numbers from 0 to n
- Triangular Numbers
- Express a number as sum of consecutive numbers
- Numbers whose factorials end with n zeros
- Check if a number can be written as a sum of 'k' prime numbers
- Rectangular (or Pronic) Numbers
- Sum of cubes of first n even numbers
Improved By : jit_t