avr39ripe

cppisPerfectRange

Aug 10th, 2021
888
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. bool isPerfect(int num)
  4. {
  5.     int sum{0};
  6.     for (int div{ 1 }; div < num; ++div)
  7.     {
  8.         if (num % div == 0)
  9.         {
  10.             sum += div;
  11.         }
  12.     }
  13.     return sum == num;
  14. }
  15.  
  16. void printPerfectRange(int begin, int end)
  17. {
  18.     int copy;
  19.     if (begin > end) { copy = begin; begin = end; end = copy; }
  20.     for (; begin != end; ++begin)
  21.     {
  22.         if (isPerfect(begin)) { std::cout << begin << '\n'; }
  23.     }
  24. }
  25.  
  26. int main()
  27. {
  28.     printPerfectRange(1, 10000);
  29. }
RAW Paste Data