Problem 49

2347 단어 em
문제 설명:
 
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.
There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.
What 12-digit number do you form by concatenating the three terms in this sequence?
 
문제 해결:
 
public class Problem49 {

	public static boolean[] prime = new boolean[10000];
	
	public static boolean IsPrime(long number) {
		int begin = 2;
		int end = (int) Math.sqrt(number) + 1;
		for (int i = begin; i < end; i++) {
			if (number % i == 0)
				return false;
		}
		return true;
	}
	
	public static int[] find_prime(){
		int[] numbers = new int[5000];
		Arrays.fill(numbers, 0);
		
		int index = 0;
		for(int i=1001; i<10000; i=i+2){
			if(IsPrime(i)){
				numbers[index] = i;
				index++;
			}
		}
		return numbers;
	}
	
	public static boolean IsNumber(int number){
		int tmp = number;
		int ge = number%10;
		number = number/10;
		int shi = number%10;
		number = number/10;
		int bai = number%10;
		number = number/10;
		int qian = number;
		number = tmp;
		//1487, 4817, 8147
		int number1 = bai*1000+shi*100+qian*10+ge;
		int number2 = shi*1000+qian*100+bai*10+ge;
		System.out.println("number:"+number+",number1:"+number1+",number2:"+number2);
		if(!prime[number1]||!prime[number2]){
			return false;
		}else{
			if(number1-number==number2-number1){
				return true;
			}else{
				return false;
			}
		}
		
	}
	
	public static void main(String[] args){
		Arrays.fill(prime, false);
		for(int i=1001; i<10000; i++){
			if(IsPrime(i)){
				prime[i] = true;
			}
		}
		for(int i=1001; i<prime.length; i++){
			if(prime[i]){
				if(IsNumber(i)){
					System.out.println(i);
				}
			}
		}
	}
}

좋은 웹페이지 즐겨찾기