Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created March 14, 2026 10:40
Show Gist options
  • Select an option

  • Save thinkphp/f6c75ae0d4d5371a01615781a99c5ac0 to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/f6c75ae0d4d5371a01615781a99c5ac0 to your computer and use it in GitHub Desktop.
teorema-fundamentala-arit.cpp
/*Teorema fundamentala a aritmeticii
orice numar poate sa fie descompus in produs de factori primi
10 = 2^1 * 5^1
11 = 11^1
12 = 2^2 * 3
1 = 2^0 * 3^0 * 5^0 =
14 = 2 * 7
10 = 2 * 5
*/
#include <iostream>
#include <vector>
using namespace std;
vector<pair<int,int>> decompuneInFactoriPrimi( int n ) {
vector<pair<int, int>> factori;
int divisor = 2;
//n = 100
while( n > 1 ) {
int count = 0;
while(n % divisor == 0) {//5%5 = 1 rest 0
n /= divisor; //n = 1
count++;//2
}
if(count > 0) {//2>0
factori.push_back({divisor, count});//in vectorul factori facem push la perechea {5,2}
}
divisor++;//divisor 5
//optimizare: verificam doar pana la radical din n
if(divisor * divisor > n) {
if(n > 1) {
factori.push_back({n,1});
}
break;
}
}
return factori; //({2,2},{5,2})
}
int main() {
int n;
cout<<"Numar=";
cin>>n;
if(n <= 1) {
cout<<"Numarul trebuie sa fie mai mare decat 1!";
return 0;
}
auto factori = decompuneInFactoriPrimi(n);
//n = 100
//factori = descompuneInFactoriPrimi(100)
//100 = 2^2 * 5^2
//output: ({2,2},{5,2})
cout<<"Descompunerea in factori primi este: ";
for(int i = 0; i < factori.size(); i++) {
cout<<factori[i].first <<"^"<<factori[i].second; //{2-first,3-second}{8-first,9-second}
if(i < factori.size() - 1) {
cout<<" * ";
}
}
cout<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment