"Programming thinking" from top to bottom gradually refinement
process-oriented programming idea: from top to bottom gradually refinement
start with the process of program execution, refine the complete process into multiple child processes, and then refine the child process until the code is complete.
Let's look at a program requirement:
Verification Goldbach conjecture : any even number greater than 6 can be decomposed into the number of two prime numbers. A prime number, which means that there are no other factors except 1 and itself.
Requirement: Enter an integer that outputs the number of the two prime numbers that can be decomposed into.
For example: Enter 14
14=3+11
14=7+7
This is a more complicated procedure. After you get the requirements of this program, you should first design the general idea of the program.
The basic ideas are as follows:
1, read an integer n
2. Split this integer into two numbers a, B, and
3. Determine if A is a prime number
4, to determine whether B is prime
5, if 3, 42 judgments are true, then output A and B
6, if this integer can also split, then go back to step 2nd. Otherwise, the program exits.
Obviously, the 2~6 step is a loop that adjusts the structure
As follows: Reads an integer n loop (splits the integer n into two integers a and b) {
To determine whether a is a prime number
To determine whether B is a prime number
If A and B are prime numbers, output A and B
}
In the basic idea above, we can see that "judging whether a is a prime number" and "Determining whether B is a prime number" is basically the same two-step operation. So, obviously, here we should write a function that can tell if an integer is a prime number.
Import Java.util.Scanner;
public class testgoldbach{public
static void Main (String args[]) {
//read int
Scanner sc = new Scanner (system.in );
int n = sc.nextint ();
loop {
int a = first integer
int b = Second integer
if (IsPrime (a) && IsPrime (b)) {
System.out.println (n + "= + A +" + "+ b);
}}} Determine whether an integer is a prime number public
static Boolean isprime (int a) {
}
}
Continue to refine. Now our focus is on two:
1. How to divide an integer n into two integers a and b;
2, how to determine an integer is prime.
First, let's look at the logic of the split. If you can determine an integer a, then another integer B is also determined that you can calculate the value of B by the expression B = n–a. So if you give an integer, how do you determine the value of a? We can look at an example first. Assuming N is 14, then all the splits are split by:
1 + 13
2 + 12
3 + 11
4 + 10
5 + 9
6 + 8
7 + 7
Down is the repetition of the demolition method. So, we take the first number as a, then a from 1 to 7, which is the change to 14/2. So our split-number loops can be analyzed:
for (int i = 1; i<=n/2; i++) {
int a = i;
int b = n-i;
if (IsPrime (a) && IsPrime (b)) {
System.out.println (n + "=" + A + "+" + B);
}
}
At this point, the main function is complete. Next, complete the IsPrime method. For how to determine whether an integer is a prime, we still use Top-down, step-by-step refinement of the way. Because of the prime number, which means that there are no other factors except 1 and itself, it is possible to judge whether an integer A is a prime number, as long as there is no factor a in the range of 2~a-1.
Therefore, the main ideas are as follows:
Cycle i:2~a-1{
If I is a factor of a, then a is not prime number} Loop end
It means that 2~a-1 is not a factor, so A is a prime number.
The complete code is as follows:
Package P5;
Import Java.util.Scanner;
public class testgoldbach2{public
static void Main (String args[]) {
//Read integer
System.out.println ("Please enter an integer") ;
Scanner sc = new Scanner (system.in);
int n = sc.nextint ();
for (int i = 1; i<=n/2; i++) {
int a = i;
int b = n-i;
if (IsPrime (a) && IsPrime (b)) {
System.out.println (n + "=" + A + "+" + B);
}}} Determine whether an integer is a prime number public
static Boolean isprime (int a) {for
(int i=2; i<= A-1; i++) {
if (a% i = = 0) return F alse;
}
return true;
}
The results of the operation are as follows: