JosepRivaille

P55307: Nombres amics

Apr 9th, 2015
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int suma_divisors(int a) {
  6.     int suma = 0;
  7.     int divisor = 1;
  8.     while (divisor <= a/2) {
  9.         if (a%divisor == 0) {
  10.             suma = suma + divisor;
  11.         }
  12.         ++divisor;
  13.     }
  14.     return suma;
  15. }
  16.  
  17. bool son_amics(int a, int b) {
  18.     if (a == b) return false;
  19.     else {
  20.         if (suma_divisors(a) == b && suma_divisors(b) == a) return true;
  21.         return false;
  22.     }
  23. }
  24.  
  25. int main() {
  26.     int a, b;
  27.     bool primer = true;
  28.     while (cin >> a >> b) {
  29.         if (son_amics(a, b)) {
  30.             if (primer) cout << '(' << a << ' ' << b << ')';
  31.             else cout << ",(" << a << ' ' << b << ')';
  32.             primer = false;
  33.         }
  34.     }
  35.     cout << endl;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment