%mathpiper,def="IsCarmichaelNumber"

// Carmichael numbers are odd,squarefree and have at least 3 prime factors
5  # IsCarmichaelNumber(n_IsEven)		<-- False;
5  # IsCarmichaelNumber(_n)_(n<561)		<-- False;
10 # IsCarmichaelNumber(n_IsPositiveInteger)	<--
[
	Local(i,factors,length,carmichael);

	factors:=Factors(n);
	carmichael:=True;
	length:=Length(factors);
	if( length < 3)[
		 carmichael:=False;
	] else [
		For(i:=1,i<=length And carmichael,i++)[
			//Echo( n-1,"%",factors[i][1]-1,"=", Modulo(n-1,factors[i][1]-1) );
			If( Modulo(n-1,factors[i][1]-1) != 0, carmichael:=False );
			If(factors[i][2]>1,carmichael:=False);	// squarefree
		];
	];
	carmichael;
];

IsCarmichaelNumber(n_IsList) <-- MapSingle("IsCarmichaelNumber",n);

%/mathpiper



%mathpiper_docs,name="IsCarmichaelNumber",categories="User Functions;Number Theory;Predicates"
*CMD IsCarmichaelNumber --- test for a Carmichael number
*STD
*CALL
	IsCarmichaelNumber(n)

*PARMS

{n} -- positive integer

*DESC

This function returns {True} if {n} is a Carmichael number, also called an absolute pseudoprime.
They have the property that $ b^(n-1) % n == 1 $ for all $b$ satisfying $Gcd(b,n)==1$. These numbers
cannot be proved composite by Fermat's little theorem. Because the previous property is extremely
slow to test, the following equivalent property is tested by MathPiper: for all prime factors $p[i]$ of $n$,
$(n-1) % (p[i] - 1) == 0$ and $n$ must be square free. Also, Carmichael numbers must be odd and have
at least three prime factors. Although these numbers are rare (there are only 43 such numbers between $1$ and $10^6$),
it has recently been proven that there are infinitely many of them.

*E.G. notest

In> IsCarmichaelNumber(561)
Result: True;
In> EchoTime() Select(1 .. 10000, IsCarmichaelNumber)
	504.19 seconds taken
Result: {561,1105,1729,2465,2821,6601,8911};

*SEE IsSquareFree, IsComposite
%/mathpiper_docs