Cod sursa(job #191692)

Utilizator cata00Catalin Francu cata00 Data 27 mai 2008 22:51:16
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#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;
}