Advertisement
rizky_herucakra

sum of divisors

Jul 5th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  SumOfDivisor
  4. //
  5. //  Created by Rizky Herucakra on 7/5/13.
  6. //  Copyright (c) 2013 Rizky Herucakra. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <vector>
  11. #include <numeric>
  12.  
  13. using namespace std;
  14.  
  15. vector<int> get_divisor_of(int val){
  16.     vector<int> result;
  17.     for (int i = 1; i <= val; ++i){
  18.         if ( (val % i) == 0) result.push_back(i);
  19.     }
  20.     return result;
  21. }
  22.  
  23. int sum_of(const vector<int>& v){
  24.     return accumulate(v.begin(), v.end(),0);
  25. }
  26.  
  27. int main(int argc, const char * argv[])
  28. {
  29.     cout << "following number [1- 1000] is equal with it's sum of divisor :" << endl;
  30.    
  31.     for (int i = 1; i <= 100; ++i){
  32.         int sumval = sum_of(get_divisor_of(i));
  33.         cout << " i = " << i << " sum of divisor = " << sumval << endl;
  34.     }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement