Dưới đây là một chương trình để tìm GCD của N số người dùng nhập vào
#include int main() { printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); int x, y =- 1; printf("Enter numbers. To exit enter 0\n"); while(1) // infinite loop to take input { scanf("%d", &x); if(x < 1) break; else if(y ==- 1) // only 1 number entered, its GCD is itself y = x; else if(x < y) y = gcd(x, y); else y = gcd(y, x); } printf("\n\n\nGCD of all the entered number is: %d", y); printf("\n\n\t\t\tCoding is Fun !\n\n\n"); return 0; } // GCD of 2 numbers is calculated at a time int gcd(int a, int b) { int i; /* a is the smallest of the two numbers of which GCD is to be calculated */ for(i = a; i >= 1; i--) { // Greatest number that divides both the numbers if(a%i == 0 && b%i == 0) break; // exits the loop } return i; }đầu ra
Chương trình C để tìm GCD của hai số bằng hàm
Dưới đây là chương trình tìm GCD của hai số bằng hàm
#include int gcd(int c, int d) // function definition { if(d == 0) return c; else return gcd(d, c%d); } int main() { printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); int a, b; printf("Enter 2 numbers: \n\n"); scanf("%d%d", &a, &b); printf("\n\nGreatest Common Divisor is: %d", gcd(a, b)); // function calling printf("\n\n\t\t\tCoding is Fun !\n\n\n"); return 0; }đầu ra
Trong toán học, ước chung lớn nhất (gcd) của hai hay nhiều số nguyên, khi ít nhất một trong số chúng khác 0, là số nguyên dương lớn nhất là ước của cả hai số. Ví dụ: GCD của 8 và 12 là 4
Ước chung lớn nhất còn được gọi là ước chung lớn nhất (gcf), thừa số chung cao nhất (hcf), đo lường chung lớn nhất (gcm) hoặc ước số chung cao nhất
Cho N số nguyên chưa biết giá trị (ai > 0) có tích P. Nhiệm vụ là tìm ước chung lớn nhất có thể lớn nhất có thể của N số nguyên này
ví dụ.
Input : N = 3, P = 24 Output : 2 The integers will have maximum GCD of 2 when a1 = 2, a2 = 2, a3 = 6. Input : N = 2, P = 1 Output : 1 Only possibility is a1 = 1 and a2 = 1.Khuyến khích. Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp
Tiếp cận.
- Đầu tiên tìm tất cả các thừa số nguyên tố của sản phẩm P và lưu trữ nó trong Hashmap
- N số nguyên sẽ có GCD tối đa khi một thừa số nguyên tố sẽ chung trong tất cả các số nguyên
- Vì vậy, nếu P = p1k1 * p2k2 * p3k3 …. trong đó p1, p2 … là các số nguyên tố thì GCD tối đa có thể đạt được sẽ là ans = p1k1 / N * p2k2 / N * p3k3 / N ….
Dưới đây là việc thực hiện các phương pháp trên.
C++
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find maximum GCD
// of N integers with product P
int 20int 22int 24
25
26int 28
26// C++ implementation of above approach0
26// C++ implementation of above approach2int___// C++ implementation of above approach4_______102_______// C++ implementation of above approach6
26// C++ implementation of above approach8
26#include <bits/stdc++.h>0 #include <bits/stdc++.h>1int #include <bits/stdc++.h>3
#include <bits/stdc++.h>4#include <bits/stdc++.h>5 #include <bits/stdc++.h>6
#include <bits/stdc++.h>7#include <bits/stdc++.h>8
#include <bits/stdc++.h>7using0
#include <bits/stdc++.h>4using2
26using2
26using6 using7
#include <bits/stdc++.h>4using9
26namespace1
26____98_______3
26#include <bits/stdc++.h>0 #include <bits/stdc++.h>1namespace7 namespace8
#include <bits/stdc++.h>4std;0std;1std;2
26std;4 std;5
using2
std;7
int std;9
25
26int // Function to find maximum GCD3
26// Function to find maximum GCD5
26std;4 // Function to find maximum GCD8
using2
Java
// of N integers with product P0
// of N integers with product P1 // of N integers with product P2
// of N integers with product P3 // of N integers with product P4
25
// Function to find maximum GCD
// of N integers with product P
// of N integers with product P8 int 20int 22int 24
25
26int int8int9200
26// C++ implementation of above approach0
26204
205206 207
26// C++ implementation of above approach8
26#include <bits/stdc++.h>0 #include <bits/stdc++.h>1int 214215216
#include <bits/stdc++.h>4#include <bits/stdc++.h>5 219220_______3_______21
#include <bits/stdc++.h>7using6224225226
#include <bits/stdc++.h>7228int9230
#include <bits/stdc++.h>7232
#include <bits/stdc++.h>7234int9236
#include <bits/stdc++.h>7
#include <bits/stdc++.h>7using0
#include <bits/stdc++.h>4using2
26using2
26using6 246int9_______3_______26
#include <bits/stdc++.h>7using6251225226
#include <bits/stdc++.h>7255int9230
#include <bits/stdc++.h>7232
#include <bits/stdc++.h>7261int9236
26namespace1
26____98_______3
#include <bits/stdc++.h>4269
270
271_______96_______0 273
271____3_______5
276
#include <bits/stdc++.h>4278
#include <bits/stdc++.h>4using2
26std;4 std;5
using2
std;7
286 // of N integers with product P8 288 289
25
26int 293294295296200
26299
using2
using2
// C++ implementation of above approach02
Python3
// C++ implementation of above approach03
// C++ implementation of above approach04
// C++ implementation of above approach05 // C++ implementation of above approach06// of N integers with product P1 // C++ implementation of above approach08
// C++ implementation of above approach09
// C++ implementation of above approach10
// C++ implementation of above approach11 // C++ implementation of above approach12
26// C++ implementation of above approach14// C++ implementation of above approach15 int9
26// C++ implementation of above approach18
26// C++ implementation of above approach20// C++ implementation of above approach15 // C++ implementation of above approach22
26
26// C++ implementation of above approach25
26#include <bits/stdc++.h>0 // C++ implementation of above approach28_______95_______29 // C++ implementation of above approach30#include <bits/stdc++.h>1215// C++ implementation of above approach4int___// C++ implementation of above approach35// C++ implementation of above approach36 int9// C++ implementation of above approach38