Advertisement
Solingen

z7.2.cpp

Dec 22nd, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. long long sumOfDivisors(long long x)
  5. {
  6.     if (x < 2) return 0;
  7.     long long sum = 1; // 1 - делитель
  8.     for (long long d = 2; d*d <= x; d++)
  9.     {
  10.         if (x % d == 0)
  11.         {
  12.             sum += d;
  13.             long long pair = x / d;
  14.             if (pair != d) sum += pair;
  15.         }
  16.     }
  17.     return sum;
  18. }
  19.  
  20. bool isPerfect(long long x)
  21. {
  22.     if (x < 2) return false;
  23.     return (sumOfDivisors(x) == x);
  24. }
  25.  
  26. int main()
  27. {
  28.     int N;
  29.     cout << "Сколько совершенных чисел вывести? ";
  30.     cin >> N;
  31.  
  32.     int found = 0;
  33.     long long c = 2;
  34.     while (found < N)
  35.     {
  36.         if (isPerfect(c))
  37.         {
  38.             cout << c << " — совершенное\n";
  39.             found++;
  40.         }
  41.         c++;
  42.     }
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement