# 110702 Carmichael Numbers

import java.util.*;

import static java.lang.Math.*;

public class Main {
private static boolean IsPrime(long number)
{
long mid = (long)(sqrt((double)number));
for (long i = 2; i <= mid; ++i)
{
if ((number % i) == 0)
return false;
}

return true;
}

private static long CarmichaelMod(long a, long powers, long number)
{
if (powers == 1)
return a % number;
if (powers == 2)
return (a * a) % number;

long result = CarmichaelMod(a, powers/2, number);
result = (result * result) % number;

if ((powers % 2) != 0)
result = (result * a) % number;
return result;
}

private static boolean MeetCarmichaelRule(long number, long a)
{
return CarmichaelMod(a, number, number) == a;
}

private static boolean IsCarmichaelNumber(long number)
{
if (IsPrime(number))
return false;

for (long i = 2; i < number; ++i)
{
if (!MeetCarmichaelRule(number, i))
return false;
}

return true;
}

private static boolean IsCarmichaelNumber(HashSet<Long> allCarmichaelNumbers, long number)
{
return allCarmichaelNumbers.contains(number);
}

private static HashSet<Long> GetAllCarmichaelNumbers(long limit)
{
HashSet<Long> result = new HashSet<Long>();
for (long i = 4; i < limit; ++i)
{
if (IsCarmichaelNumber(i))
}

return result;
}

public static void main(String[] args) {
HashSet<Long> allCarmichaelNumbers = GetAllCarmichaelNumbers(65000);
Scanner in = new Scanner(System.in);
while (true)
{
long number = in.nextLong();
if (number == 0)
return;
if (IsCarmichaelNumber(allCarmichaelNumbers, number))
System.out.println("The number " + number + " is a Carmichael number.");
else
System.out.println(number + " is normal.");
}
}

}


• 本文已收录于以下专栏：

举报原因： 您举报文章：110702 Carmichael Numbers 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)