# 110702 Carmichael Numbers

238人阅读 评论(0)

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.");
}
}

}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：21375次
• 积分：733
• 等级：
• 排名：千里之外
• 原创：55篇
• 转载：0篇
• 译文：0篇
• 评论：0条
文章分类
阅读排行
评论排行