#include <iostream>
#include <chrono>  
using namespace std;
using namespace std::chrono; 

long long sumMultiplesLoop(int n) { // long long is like int but for big numbers 
    long long sum = 0; 
    for (int i = 1; i <= n; ++i) {
        if (i % 3 == 0 || i % 5 == 0) {
            sum += i;
        }
    }
    return sum;
}

long long sumOfMultiples(int n, int k) { //known formula: k*m*(m+1)/2 for multiples of k leq n
    long long m = n / k;            // how many multiples
    return (long long)k * m * (m + 1) / 2;
}

// Formula version (no loop)
long long sumMultiplesFormula(int n) {
    return sumOfMultiples(n, 3) + sumOfMultiples(n, 5) - sumOfMultiples(n, 15); //subtract multiples of 15 because they count double
}

int main() {
    int n = 1432987;

    auto start1 = high_resolution_clock::now();   
    long long result1 = 0;
    for (int i = 0; i < 1000; ++i) {             
        result1 = sumMultiplesLoop(n);
    }
    auto end1 = high_resolution_clock::now();    
    duration<double> average1 = (end1 - start1) / 1000; 

    auto start2 = high_resolution_clock::now();
    long long result2 = 0;
    for (int i = 0; i < 1000; ++i) {
        result2 = sumMultiplesFormula(n);
    }
    auto end2 = high_resolution_clock::now();
    duration<double> average2 = (end2 - start2) / 1000;
    
    cout << "Loop result: " << result1 << endl;
    cout << "Loop average time: " << average1.count() << " seconds" << endl;
    cout << "Formula result: " << result2 << endl;
    cout << "Formula average time: " << average2.count() << " seconds" << endl;

    return 0;
}

