Pagini recente » Cod sursa (job #1285804) | Cod sursa (job #240074) | Monitorul de evaluare | Cod sursa (job #2022931) | Cod sursa (job #191692)
Cod sursa(job #191692)
#include <stdio.h>
// Based on the fact that 1/5 + 1/25 + 1/125... converges to 1/4.
// Search for the solution between p * 4 - 100 and p * 4 + 100.
int p;
int compute(int n) {
int result = 0;
while (n) {
result += n/5;
n /= 5;
}
return result;
}
int main(void) {
FILE* f = fopen("fact.in", "rt");
fscanf(f, "%d", &p);
fclose(f);
int result = -1;
if (p == 0) {
result = 1;
} else {
int n = p * 4 / 5 * 5 - 100; // multiple of 5
if (n < 0) {
n = 0;
}
int high = n + 200;
while (n <= high && result == -1) {
if (compute(n) == p) {
result = n;
}
n += 5;
}
}
f = fopen("fact.out", "wt");
fprintf(f, "%d\n", result);
fclose(f);
return 0;
}