Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "string.h"
- #include "array.h"
- using namespace std;
- // Сумма собственных делителей
- long long sumOfDivisors(long long x)
- {
- if (x < 2) return 0;
- long long sum = 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()
- {
- cout << "Сколько совершенных чисел вывести? ";
- int N;
- cin >> N;
- // Сохраним найденные совершенные числа в Array<long long>
- Array<long long> perfects;
- long long candidate = 2;
- while (perfects.size() < N)
- {
- if (isPerfect(candidate))
- {
- perfects.append(candidate);
- }
- candidate++;
- }
- // Выведем их, используя String
- for (int i = 0; i < perfects.size(); i++)
- {
- String msg = "Совершенное #";
- msg = msg + to_string(i+1).c_str();
- msg = msg + ": ";
- msg = msg + to_string(perfects[i]).c_str();
- cout << msg << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement