Io ho fatto un programma in C++ che fattorizza 3 numeri interi e ne ricava mcm e MCD, ti posto il codice per farti vedere come ho risolto:
codice:#include <iostream.h> #include <iomanip.h> int MCD(int, int, int); int mcm(int, int, int); int main() { int a,b,c; bool is; int co = 0; cout << "Questo programma fattorizza tre numeri e ne ricava mcm e MCD\nInserisci tre numeri interi:\n"; cin >> a; cin >> b; cin >> c; int numfirst[5000]; for(int i = 2; i < 5000; i++) { is = true; for(int j = 2; j < i/2; j++) if((i%j) == 0) is = false; if(is) numfirst[co] = i; co++; } int now, nowb, nowc; now = a; for(int n = 0; n <= co; ) { if(now % numfirst[n] == 0) { cout << endl << now << ":"; cout << numfirst[n] << "\r"; now /= numfirst[n]; n = 0; } else n++; } nowb = b; cout << endl; for(int o = 0; o <= co; ) { if(nowb % numfirst[o] == 0) { cout << endl << nowb << ":"; cout << numfirst[o] << "\r"; nowb /= numfirst[o]; o = 0; } else o++; } nowc = c; cout << endl; for(int p = 0; p <= co; ) { if(nowc % numfirst[p] == 0) { cout << endl << nowc << ":"; cout << numfirst[p] << "\r"; nowc /= numfirst[p]; p = 0; } else p++; } cout << endl; cout << "\nmcm(" << a << ", " << b << ", " << c << ")" << " = " << mcm(a, b, c) << endl; cout << "MCD(" << a << ", " << b << ", " << c << ")" << " = " << MCD(a, b, c) << endl << endl; cout << "Premi un tasto per uscire: "; char exit; cin >> exit; return 0; } int mcm(int a, int b, int c) { for(int mcm = 2; ; mcm++) { if(mcm % a == 0 && mcm % b == 0 && mcm % c == 0) break; } return mcm; } int MCD(int a, int b, int c) { int max; if(a > b && a > c) max = a; else if(b > a && b > c) max = b; else if(c > a && c > b) max = c; for(int MCD = max; ; MCD--) { if(a % MCD == 0 && b % MCD == 0 && c % MCD == 0) break; } return MCD; }![]()

Rispondi quotando