110702 Carmichael Numbers

原创 2013年12月02日 14:05:48


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

}


programming-challenges Carmichael Numbers (110702) 题解

2 hints:  need long long to calculate multiple-mode, int may overflow;check whether the number ...

UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some peopl...

Uva 10006 Carmichael Numbers(数论、快速幂、素数筛法)

Uva 10006 Carmichael Numbers(数论、快速幂、素数筛法)

UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some...

UVa 10006 - Carmichael Numbers

/* 数学:素数 + 模运算 Carmichael Number: 1)非素数2)通过 Fermat Pass 所以,程序要完成两个工作。 1) 判断n是否为素数 2) 测试对于 2 ...
  • cugfjp
  • cugfjp
  • 2013年07月16日 16:20
  • 307

Carmichael Numbers

#include #include using namespace std; int main(int ac,char *av[]) { int arr[15]={561, 1105, 172...

UVa NO.10006 Carmichael Numbers

Carmichael Numbers Time limit3000 ms Description An important topic nowadays in computer science ...
  • Murphyc
  • Murphyc
  • 2017年05月21日 21:24
  • 106

UVa 10006 Carmichael Numbers(数论&卡米歇尔数&考塞特判别法)

10006 - Carmichael Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com...

UVA - 10006 Carmichael Numbers - 快速幂

UVA - 10006 Carmichael Numbers- 快速幂An important topic nowadays in computer science is cryptography. ...
  • ly59782
  • ly59782
  • 2017年04月07日 14:18
  • 124

uva10006 Carmichael Numbers(卡迈克尔数+素数打表)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:110702 Carmichael Numbers
举报原因:
原因补充:

(最多只允许输入30个字)