Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- long long sumOfDivisors(long long x)
- {
- if (x < 2) return 0;
- long long sum = 1; // 1 - делитель
- for (long long d = 2; d*d <= x; d++)
- {
- if (x % d == 0)
- {
- sum += d;
- long long pair = x / d;
- if (pair != d) sum += pair;
- }
- }
- return sum;
- }
- bool isPerfect(long long x)
- {
- if (x < 2) return false;
- return (sumOfDivisors(x) == x);
- }
- int main()
- {
- int N;
- cout << "Сколько совершенных чисел вывести? ";
- cin >> N;
- int found = 0;
- long long c = 2;
- while (found < N)
- {
- if (isPerfect(c))
- {
- cout << c << " — совершенное\n";
- found++;
- }
- c++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement