Java implementation of the algorithm improves the Blue Bridge Cup Goldbach's Conjecture

Questions algorithms improve Goldbach's Conjecture

Resource limitation
time limit: 1.0s memory limit: 256.0MB
Description of the problem
  according to the function (determining whether an integer is a prime number), and then relying on this function, the even input (6-N) in N, the output of two primes and (required for decomposing the first viable), and verify each Goldbach conjecture: any even number greater than or equal to 6 (even number can be verified between 6 to 0x7FFFFF) can be expressed as the sum of two primes, attention here are not required to verify the Goldbach conjecture.
Input Format
  input test data will meet format.
  7
the output format
  required to meet the user's output format.
  3 + 3 = 6
sample input
an input example to meet the requirements of the subject.
Example:
30
sample output from
the sample corresponding to the above input and output.
Example:
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7
12 = 5 + 7
14 = 3 + 11
16 = 3 + 13
18 = 5 + 13
20 = 3 + 17
22 = 3 + 19
24 = 5 + 19
26 3 + 23 =
28 = 23 + 5
data size and conventions
  range of each number of the input data.
  Example: 0 <n, m <100 , 0 < = the number of each matrix <= 1000.

import java.util.ArrayList;
import java.util.Scanner;

public class 哥德巴赫猜想2 {
public static	ArrayList<Integer> list = new ArrayList<Integer>();
public static int n=0;
public static String [] num    ;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		sc.close();
		 num = new String [n];
		zhishu();
		f(0,0);
		for (int i = 6; i <n; i+=2) {
			System.out.println(i+num[i]);
		}
	}
	public static void f(int start,int end){
		if(end>=list.size() || start>=list.size()) return;
		int temp = list.get(start)+list.get(end);
		if(temp>=n ) return;
		if(num[temp]==null)
		num[temp]="="+list.get(start)+"+"+list.get(end);
		f(start,++end);
		f(++start,end);
	}
	public static void zhishu(){
	A:	for (int i = 2; i <n; i++) { 
			int sqrt=(int) Math.sqrt(i);
			for(int num:list){
				if(i%num==0){
					continue A;
				}
				else if(num>sqrt)
					break;
			}
			list.add(i);
		}
	}

}

发布了1448 篇原创文章 · 获赞 1万+ · 访问量 170万+

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104724701