Pagini recente » Cod sursa (job #2645134) | Cod sursa (job #1886109) | Cod sursa (job #350413) | Cod sursa (job #1106889) | Cod sursa (job #2907383)
// Infoarena.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <iostream>
#include <fstream>
#include <math.h>
int factorial(int targetZeroes = 0) {
if (targetZeroes == 0 || targetZeroes < 0) {
return 1;
}
int n5 = 0;
int n10 = 0;
int index = 5;
do {
if (n10 + n5 > targetZeroes) {
return -1;
}
int current = index;
/*while (current % 10 == 0) {
n10 += 1;
current /= 10;
}*/
double l10 = log10(current);
if (l10 == floor(l10)) {
n10 += l10;
current = l10;
}
double log5 = log2(current) / log2(5);
if (log5 == floor(log5)) {
n5 += log5;
}
else if (current % 5 == 0) {
n5 += 1;
}
index += 5;
} while (n10 + n5 != targetZeroes);
return index - 5;
}
int main()
{
//std::cout << "Executing..." << std::endl;
std::ifstream in;
in.open("fact.in");
int target = 0;
in >> target;
in.close();
//std::cout << "Computing factorial for " << target << " number of zeroes" << std::endl;
int result = factorial(target);
//std::cout << "Result is " << result << std::endl;
std::ofstream out;
out.open("fact.out");
out << result;
out.close();
return 0;
//std::cout << "Done!" << std::endl;
}