Returns the maximum common divisor of two positive integers -- moving and Subtraction

Source: Internet
Author: User

Problem: solving the maximum common divisor of two positive integers

In the first section of the formal method class today, we will give a simple example: finding the maximum common divisor of two positive integers by moving and subtraction to explain the basic content of the formal method and give us a perceptual knowledge. The basic idea is as follows:

1. Given any two positive integers A and B;

2. If a and B are not equal, perform step 1;

3. Select the greater one in A and B, and assign the difference between the greater one and the smaller one to the greater one;

4. Check whether the values of A and B are equal after the value is re-assigned. If the values are not equal, continue with Step 1; otherwise, execute step 2;

5. Return A or B.

The program is as follows (written in Java ). The predicate and post-asserted statements in the program are the content of the formal method. The predicate determines whether the data used for calculation meets the requirements. The latter asserted that the program execution results are correct. The first time I came into contact with the formal method, I was not very familiar with it. It was quite interesting.

1 package COM. luop. algorithm; 2 3/** 4 * calculate the maximum common approx. For two positive integers, 5 * @ author luopeng 6*7 */8 public class greatestcommondivisor {9 10/** 11*12 * @ Param first positive integer 13 * @ Param second positive integer 14 * @ return 0: first or second has a non-positive integer;-1: an error occurs, and the returned positive integer is not the maximum public approx. Otherwise, the return value is the maximum public approx. 15 */16 public int continuousminus (final int first, final int second) {17 18/* 19 * predicate: judge whether the input has a non-positive integer 20 */21 if (first <= 0) | 22 (second <= 0) {23 return 0; 24} 25 26/* 27 * calculate the maximum approximate number of the moving phase Subtraction Method. We calculated 28 */29 int temp1 = first; 30 int temp2 = Second; 31 While (temp1! = Temp2) {32 If (temp1> temp2) {33 temp1 = temp1-temp2; 34} else {35 temp2 = temp2-temp1; 36} 37} 38 39/* 40 * post asserted: ensure the correctness of the Program Results 41 * For any positive integer a, if a satisfies (first % A = 0) & (second % A = 0), there are all temp1> = a42 */43 int COUNT = (first <second )? First: Second; // take the smaller number of first and second 44 for (INT I = 1; I <= count; I ++) {45 if (first % I = 0) & 46 (second % I = 0) {47/* 48 * If temp1 is less than a certain public approx of first and second, the obtained result is incorrect. 49 */50 if (temp1 <I) {51 return-1; 52} 53} 54} 55 56/* 57 * returned result 58 */59 return temp1; 60 61} 62 63}

 

Returns the maximum common divisor of two positive integers -- moving and Subtraction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.