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 firstname.lastname@example.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.
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Numbers less than N which are product of exactly two distinct prime numbers
- Numbers within a range that can be expressed as power of two numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Count numbers which are divisible by all the numbers from 2 to 10
- Count numbers which can be constructed using two numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Add two numbers using ++ and/or --
- Sum of first n even numbers
- Sum of numbers from 1 to N which are divisible by 3 or 4
- Sum of all even numbers in range L and R
- Sum of sum of first n natural numbers
Improved By : jit_t