c++ - Sum of all divisors: Any way to optimize this code? -


this code calculate sum of divisors of given number. runs pretty already, question is: can optimize more , there maybe better algorithm this?

here's code:

#include <iostream> #include <chrono> #include <cmath>  using namespace std; using namespace chrono;  typedef high_resolution_clock hr_clock;  void friendnumbers(int max); inline int sumofdivisors(int* n);  int main() {     auto max = 0;     cout << "please enter maximum number: " << endl;     cin >> max;      hr_clock::time_point t1 = hr_clock::now();     friendnumbers(max);     hr_clock::time_point t2 = hr_clock::now();      auto duration = duration_cast<milliseconds>(t2 - t1).count();     cout << "the program took " << duration << " milliseconds complete." << endl; }  //here check, if sum1 , sum2 friend numbers //(explanation beneath) , print them out void friendnumbers(int max) {        std::ios::sync_with_stdio(false);     for(int = 2; != max; ++i) {         auto sum1 = sumofdivisors(&i);         if(i < sum1) continue;         auto sum2 = sumofdivisors(&sum1);         if(i == sum2 && sum1 != sum2) {             cout << sum1 << " | " << sum2 << endl;         }     } }  //this function returns sum of divisors of n inline int sumofdivisors(int* n) {     auto sum = 1;     auto border = round(sqrt(*n)+1);     for(int = 2; < border; ++i)         if(*n % == 0 && i*i != *n)             sum += + (*n / i);     return sum; } 

with sum of divisors calculate friend numbers, eg. 220 , 284 because sum of divisors of 220 equals 284 , other way round.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -