Advertisement
Miketo_prog

Factores primos

Jun 9th, 2020 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int facts(int, int);
  5. bool esPrimo(const int);
  6.  
  7. int main()
  8. {
  9.     int n, m;
  10.    
  11.     cout<<"Introduce un número entero"<<endl;
  12.     cin>>n;
  13.    
  14.     cout<<"\n\nLos factores primos de "<<n<<" en orden decreciente son:"<<endl;
  15.    
  16.     if(esPrimo(n)) cout<<n<<endl;
  17.     m=facts(n, n/2);
  18.    
  19.     return 0;
  20. }
  21.  
  22. int facts(int num, int q){
  23.     if( q==0 ) return 0;
  24.     if( num%q==0 && esPrimo(q) ) cout<<q<<endl;
  25.     return facts(num, q-1);
  26. }
  27.  
  28. bool esPrimo(const int num){
  29.     if( num<0 )
  30.         return 0;
  31.        
  32.     for(int i=2; i<=(num/2); i++)
  33.         if( num%i==0 )
  34.             return 0;
  35.            
  36.     return 1;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement