Mencari gcd terbesar, lebih dari 2 angka , Pakai Bahasa C

GGCD

Euclidean algorithm is an efficient method for computing greatest common divisor (GCD) of 2 numbers, the largest positive integer that divides both of them without leaving a remainder. For example, the GCD of 8 and 12 is 4. To calculate the GCD, we could use the equation below:

GCD(a,b) = GCD(b,a) if a < b GCD(a,b) = GCD(b,a%b) if a > b and b != 0 GCD(a,b) = a if a > b and b = 0

This problem is very simple, you just have to read N number and print the greatest of GCD (GGCD). To find GGCD, you must find the GCD of all pairs (ai, aj) where i != j and find the largest GCD.

Format Input The input starts with an integer T represents the number of test cases. Each test case will start with an integer N, the number of numbers to read. The next line will contain N integers ai as the numbers to be read.

Format Output For each test case, print "Case #X: Y" where X is the test number and Y is the GGCD.

Constraints 1 <= T <= 100 2 <= N <= 100 1 <= ai <= 1 000 000 000

Sample Input (standart input)

4 5 10 25 15 30 50 4 10 11 34 22 3 1 17 33 3 5 5 7

Sample Output (standard output) Case #1: 25 Case #2: 11 Case #3: 1 Case #4: 5

Explanation All pairs' GCD in Case #2: GCD(10, 11) = 1 GCD(10, 34) = 2 GCD(10, 22) = 2 GCD(11, 34) = 1 GCD(11, 22) = 11 GCD(34, 22) = 2 Therefore, GGCD = 11

avatar EdwardSword
@EdwardSword

1 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

Tanggapan

Terimakasih informasinya. :)

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban