Source of the topic:
Cycle-04. Verify "Goldbach conjecture" (20) time limit of MS
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions StandardAuthor Xu Jianchun (Zhejiang University)
The famous "Goldbach conjecture" in the field of mathematics roughly means that any even number greater than 2 is always represented as the sum of two primes. For example: 24=5+19, where 5 and 19 are prime numbers. The task of this experiment is to design a program to verify that the even number within 2 billion can be decomposed into a sum of two primes.
Input format:
The input gives an even n in the range of (2, 2 000 000 000] in a row.
Output format:
In a row, according to the format "n = p + q" The prime number of the output n is decomposed, where P <= Q are prime. And because such decomposition is not unique (for example, 24 can also be decomposed into 7+17), it is necessary to output all the solutions of the P smallest solution.
Input Sample:
24
Sample output:
24 = 5 + 19
Code:
Import Java.io.ioexception;import java.text.decimalformat;import Java.text.parseexception;import Java.util.Scanner ;p Ublic class Main {public static void Main (string[] args) throws ParseException, IOException { Scanner Scanner = New Scanner (system.in); Long N; a A, a, two primes long A, b; Enter a word number n = Scanner.nextlong (); Starting from (2,n] The loop to find the prime number for (long i=2; i<n; i++) { //Determine if it is a prime if (Isprimenumber (i)) { a=i; B=n-i; if (Isprimenumber (b)) { //Let large numbers row in front of if (a>b) { long temp; Temp =a; A= B; b= temp; } System.out.println (n+ "=" +a+ "+" +b); Break ; }}}} public static Boolean Isprimenumber (long N) { //1 is not a prime if (n ==1) return false; To determine if I is a prime number, simply remove each integer between 2~ radical I for (Long i = 2;I<=MATH.SQRT (n); i++) { if (n%i==0) return false; } return true; }}
Reference:
http://blog.csdn.net/summercpp/article/details/38326679
Http://zuoye.baidu.com/question/dc086c6a0655d55493b45d27b4272b4c.html
Cycle-04. Verifying "Goldbach conjecture" (20)