Cod sursa(job #2772296)

Utilizator Gabryel9898Bizdoc Vasile Gabriel Gabryel9898 Data 31 august 2021 17:19:08
Problema Factorial Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <iostream>
#include <chrono>

class Factorial {
private:
    int counter = 0;
    int number_of_2 = 0;
    int number_of_5 = 0;

    [[nodiscard]] int get_number_of(int number) const {
        int counter_copy = counter;
        int result = 0;

        while (counter_copy && counter_copy % number == 0) {
            ++result;
            counter_copy /= 5;
        }

        return result;
    }

public:
    [[nodiscard]] int findTrailingZeros() const {
        return std::min(number_of_2, number_of_5);
    }

    void next() {
        ++counter;
        number_of_5 += get_number_of(5);
        number_of_2 += get_number_of(2);
    }

    [[nodiscard]] int getInternalCounter() const {
        return counter;
    }
};


using namespace std::chrono;


int main() {
    auto start = high_resolution_clock::now();
//    auto target =  std::pow(10, 8);
    auto target = 2;
    auto f = new Factorial();
    while (f->findTrailingZeros() != target) {
        f->next();
    }
    std::cout << f->getInternalCounter();

//    for (int i = 0; i < std::pow(10, 8); ++i) {
//    }
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(stop - start);
    std::cout << std::endl << "[time]:" << duration.count() << std::endl;
    return 0;
}