#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <sstream>
#include <algorithm>
#include <numeric>
class muTimer
{
using Clock = std::chrono::high_resolution_clock;
bool active = false;
Clock::duration duration_;
Clock::time_point start_ = Clock::now(), stop_ = Clock::now();
muTimer(const muTimer&) = delete;
muTimer& operator=(const muTimer&) = delete;
public:
using ns = std::chrono::nanoseconds;
using mks = std::chrono::microseconds;
using ms = std::chrono::milliseconds;
muTimer() { reset(); start(); }
~muTimer() = default;
muTimer& reset()
{
duration_ = std::chrono::nanoseconds(0);
active = false;
return *this;
}
muTimer& start()
{
if (!active)
{
start_ = Clock::now();
active = true;
}
return *this;
}
muTimer& stop()
{
if (active)
{
stop_ = Clock::now();
duration_ += stop_ - start_;
active = false;
}
return *this;
}
template<typename T = mks>
unsigned long long duration()
{
return static_cast<unsigned long long>
(std::chrono::duration_cast<T>(stop_-start_).count());
}
};
using namespace std;
const int Count = 1000000;
unsigned int Hovsepyan()
{
unsigned int sum = 0;
for(int n = 0; n < Count; ++n)
{
n = std::abs(n);
if (n < 10) { sum += n; continue; }
std::stringstream io;
io << n;
char t;
while (io >> t)
sum += t - '0';
}
return sum;
}
unsigned int olkhovich()
{
unsigned int sum = 0;
for(int n = 0; n < Count; ++n)
{
int number = n;
while (number) {
sum += number % 10;
number /= 10;
}
}
return sum;
}
unsigned int dIm0n()
{
unsigned int sum = 0;
for(int n = 0; n < Count; ++n)
{
const auto s = to_string(n);
sum += accumulate(cbegin(s), cend(s), 0,
[](auto acc, auto x) {
return acc + (x - '0');});
}
return sum;
}
int main(int argc, const char * argv[])
{
{
muTimer mt;
unsigned int s = Hovsepyan();
mt.stop();
cout << "Hovsepyan() == " << s << " for " << mt.duration<>() << " mks\n";
}
{
muTimer mt;
unsigned int s = olkhovich();
mt.stop();
cout << "olkhovich() == " << s << " for " << mt.duration<>() << " mks\n";
}
{
muTimer mt;
unsigned int s = dIm0n();
mt.stop();
cout << "dIm0n() == " << s << " for " << mt.duration<>() << " mks\n";
}
}