All pastes #30609 Raw Edit

Mine

public text v1 · immutable
#30609 ·published 2005-11-23 16:17 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>

static inline unsigned long long
rdtsc (void)
{
  asm ("rdtsc");
}

static void educate (void);

int
main (int argc, char **argv)
{
  mpz_t n;
  unsigned long i, s;
  unsigned long long before, after;
  char **ptr;
  if (argc < 2)
    educate ();
  for (ptr = (argv + 1); *ptr != 0; ptr++)
    {
      mpz_init (n);
      mpz_add_ui (n, n, 1);
      s = strtoul (*ptr, 0, 10);
      before = rdtsc ();
      for (i = s; i > 0; i--)
	mpz_mul_ui (n, n, i);
      after = rdtsc ();
      printf ("Ticks: %llu - Factorial of %lu: %s\n", (after - before), s,
	      mpz_get_str (0, 10, n));
      mpz_clear (n);
    }
  return 0;
}

static void
educate (void)
{
  fprintf (stderr, "usage: factorial num1 ...\n");
  exit (1);
}