/*
 *  e.c:        A program to calculate e to the 8585th digit
 *  Author:     V. Alex Brennen (http://cryptnet.net/people/vab/)
 *  Date:       2004.07.22 
 *  Copyright:  Public Domain.
 */

#include <stdio.h>
#include <gmp.h>


int main(void)
{
	mpf_t c;
	mpf_t e;	
	mpf_t d;
	
	unsigned long int i = 1;
	unsigned long int j = 0;
	mpf_t k;
	FILE *out = NULL;

	if(NULL == (out = fopen("out.txt","w")))
	{
		fprintf(stderr,"Failed to open out file.\n");

		return -1;
	}
	
	/* 8192: 1/ 0.1276e285040.7838e-28503 */
	mpf_set_default_prec(28505);
	
	mpf_init2(e,28505);
	mpf_init2(c,28505);
	mpf_init2(d,28505);
	mpf_init(k);

	mpf_set_str(c,"1e0",10);
	mpf_set_str(e,"1e0",10);
	
	for(i=1;i<8193;i++)
	{
		j = i;
		mpf_init(k);
		while(j != 0)
		{
			if(!mpf_cmp_ui(k,0)){ mpf_set_ui(k,j);}
			else { mpf_mul_ui(k,k,j); }
			j--;
		}
		mpf_div(d,c,k); 
		printf("%d: %d/ ",i,1);
		mpf_out_str(NULL,10,(sizeof(10000)),k);
		mpf_out_str(NULL,10,(sizeof(10000)),d);
		printf("\n");
		mpf_add(e,e,d);
	
	}
	/* mpf_out_str 10 is base and 0 denotes precision of accurate 
           maximum */
	if(!mpf_out_str(out,10,0,e))
	{
		fprintf(stderr,"Error Writing File.\n");

		return -1;
	}
	printf("\n");
	fclose(out);

	return 0;
}