[C programming] simple programming exercises-(2) the greatest common divisor and the least common multiple

table of Contents

1. Problem description

2. Problem solving

problem analysis:

programming:

Method 1: (Exhaustive method)

Method 2: (Toss and toss and divide (also known as Euclidean algorithm))

Method three: (more phase reduction method)


1. Problem description

problem:

Find the greatest common divisor and least common multiple of m and n.

Examples:

The greatest common divisor of 2 and 4 is 2, and the greatest common multiple is 8.

enter:

2 4

Output:

4 8


2. Problem solving

problem analysis:

(1) the greatest common divisor: (also known as the greatest common factor, the greatest common factor), refers to two or more integers consensus about the number of the largest one. The greatest common divisor of a and b is denoted as (a, b) . There are many ways to find the greatest common divisor . The common ones are the prime factor decomposition method, the tossing division method , and the more phase subtraction method . The concept corresponding to the greatest common divisor is the least common multiple .

For example: [12 and 24] The divisors of 12 are: 1, 2, 3, 4, 6, 12; the divisors of 24 are: 1, 2, 3, 4, 6, 8, 12, and 24. Their common divisors are: 1, 2, 3, 4, 6, 12, and the greatest common divisor of 12 and 24 is 12.

(2) the least common multiple: two or more integers communal multiple named common multiple thereof, wherein other than 0 smallest common multiple of a least common multiple of these is called an integer. The least common multiple of integer a and b is denoted as [a, b].

For example: [12 and 24] The multiples of 12 are: 12, 24, 36, etc.; the divisors of 24 are: 24, 48, 72, etc. Their common divisors are: 24, 72, etc., then the least common multiple of 12 and 24 is 24

(3) The relationship between the least common multiple and the greatest common divisor:

Least Common Multiple=m*n/Greatest Common Divisor

 

programming:

Method 1: (Exhaustive method)

Idea: First determine which number of m and n is smaller, and use the smaller number as the starting factor (i) in the interval [i,1] to determine whether it is both divisible by m and n. If not, i--(Factor minus one) Repeat the judgment action until a factor that satisfies the condition is found, and the program ends; if it is satisfied, the factor is the greatest common factor, and the program ends directly.

#include <stdio.h>

//计算最大公因子(穷举法)
int gcd(int m, int n)
{
	int temp = m > n ? n : m;

	int res = 1;
	for (int i = temp; i > =1; i--) {
		if (m % i == 0 && n % i == 0) {
			res = i;
			break;
		}
	}
	return res;

}

int main()
{
	int m, n;
	scanf("%d%d", &m, &n);
	int temp = gcd(m, n);
	printf("%d %d\n", temp, m * n / temp);
}

Test 1:

Test 2:

Method 2: (Toss and toss and divide (also known as Euclidean algorithm))

Idea : Tossing and dividing algorithm: When the divisor is not 0, perform the remainder operation on the divisor m and the dividend n, and the remainder temp; then let m=n; n=temp; continue to determine whether the divisor n is 0, repeat the previous actions, Until the divisor is 0, the program ends.

//计算最大公因子(辗转相除法——非递归)
int gcd2_1(int m, int n)
{
	int res = 0;
	while(n != 0) {
		int temp = m % n;
		m = n;
		n = temp;
	}
	res = m;
	return res;

}
//计算最大公因子(辗转相除法——递归)
int gcd2_2(int m, int n)
{
	if (m % n == 0)
		return n;
	else
		return gcd2_2(n, m % n);

}

Method three: (more phase reduction method)

Decreases law : also called Decreases Technique , is from the " Nine Chapters on Arithmetic " the greatest common divisor algorithm, which originally was about points and design, but it applies to any request the greatest common divisor occasion.

Algorithm steps:

The first step: give two positive integers arbitrarily; judge whether they are both even numbers. If it is, then divide by 2 to reduce and re-judge; if it is not, go to the second step.

The second step: subtract the smaller number n from the larger number m to get the difference temp, then compare the difference with the smaller number n, and reduce the number by the larger number (m=larger of the two Number, n=smaller number). Repeat step two until the minus and difference are equal.

The greatest common factor = (the product of several 2 reduced in the first step) * (the equal number at the end of the second step (ie difference or subtraction))

int gcd3(int m, int n)
{
	int res1=1, x;
	while (m % 2 == 0 && n % 2 == 0)  //判断m和n能被多少个2整除,并计算若干2的乘积结果为res1
	{
		m /= 2;
		n /= 2;
		res1*=2;
	}
	
	//保证m保存较大的值
	if (m < n) {    
		int temp = m;
		m = n;
		n = temp;
	}

	do
	{
		x = m - n;
		m = (n > x) ? n : x;
		n = (n < x) ? n : x;
	} while (n != x);
	if (res1 == 1)
		return x;
	else
		return res1*x;
}

 

Guess you like

Origin blog.csdn.net/Jacky_Feng/article/details/109647746