`
to_zoe_yang
  • 浏览: 143318 次
  • 性别: Icon_minigender_2
  • 来自: 01
社区版块
存档分类
最新评论

Problem 35

 
阅读更多

问题描述:

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?

 

解决问题:

 

先记录好哪些是素数,哪些不是!

 

public class Problem35 {

	public static final int UP = 1000000;
	public static boolean[] prime = new boolean[1000000];

	public static boolean IsPrime(int number) {
		boolean result = true;

		if (number % 2 == 0) {
			result = false;
		} else {
			int middle = (int) Math.sqrt(number);
			for (int i = 3; i <= middle; i += 2) {
				if (number % i == 0) {
					result = false;
					break;
				}
			}
		}

		return result;
	}

	public static boolean IsNumber(int number) {

		boolean ok = true;
		int current = number;
		
		if (!prime[number]) {
			return false;
		}
		if(number<10)
			return true;
		
		do {
			int hight = current / 10;
			int low = current % 10;
			current = Integer.parseInt(low + "" +hight );
//			System.out.println("Current:"+current);
			if (!prime[current]) {
				return false;
			}
		} while (current != number);

		return ok;
	}

	public static int sum() {
		int result = 0;

		for (int i = 2; i < UP; i++) {

		}

		return result;
	}

	public static void main(String[] args) {
		
		long sum = 1;
		Arrays.fill(prime, false);
		for (int i = 3; i < UP; i++) {
			if(IsPrime(i))
				prime[i] = true;
		}
		for(int i=2; i<UP; i++){
			if(IsNumber(i)){
				System.out.println(i);
				sum++;
			}
		}
		System.out.println("Sum:"+sum);
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics