Thread: The number e

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    12

    Question The number e

    How could i print the number E to the 50th digit, currently it is only printing to the 7th digit. here is my code

    Code:
    #include <stdio.h>
    #define MAX 30
    
    int factorial ( int i ) ;
    int main ( void ) 
    
    	{
    	double e ;
    	int i , j ;
    
    	for (e= 1 , i =1 ; i < MAX ; i++)
    		e += 1.0/(j=factorial(i));
    	
    	printf("%f \n ",e);
    	return 0;
    	}
    
    
    int factorial(int num)
    	{
    
        	if (num == 1)
           	 	return 1;
        	else	
            	return num * factorial(num-1);
    	}
    if i make the variable e to a long double i get 0.0000000 and if I increase the MAX to lets say 40 i get inf .

    btw. i am using gcc 3.3.3 on fedora 2
    thanks
    Last edited by qodsec; 06-20-2004 at 09:55 AM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    printf("%.30f \n", e);
    However IIRC the factoral of 30 is much too large to fit inside of an int.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How could i print the number E to the 50th digit
    You use an arbitrary precision math library or do it manually. C doesn't natively support numeric quantities that long.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    12
    Prelude how do you do it manually??

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Prelude how do you do it manually??
    There are a number of ways, but usually they involve arrays or lists of either characters representing each digit with functions that manually perform mathematical operations on each digit, or integers where each integer marks a certain length of the total number. There was a contest not too long ago here that asked for something similar. If you ask nicely I'm sure some of the contestants would be happy to share their code and techniques with you.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    12
    thanks Prelude I will do a search on this website to track down the post.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    12
    Ok I found this code at http://remus.rutgers.edu/~rhoads/Code/e.c but i could barely understand the last 3 lines could someone help me.

    Code:
     
    int a[3302] , b=3301 , *c=a ,d , e , f ;
    
    main()	{
    	for(e=b;--e;*c++=1); 			/* the loop sets all elements in array a to 1 */
    	*c=2;					/* last element of array a is equal to 2     */
    	for(d=2001;d--;printf("%05d",f))	
    		for(c=a,e=b;e;f/=e--)
    			{ f+=*c*1e5 ; *c++=f%e; } 
    	}
    thanks

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Despite being poorly written (probably an attempt at obfuscation), it's pretty straightforward. Try tracing the program line by line until it's done. Then you'll have a good idea of how it goes about the calculations, from there you shouldn't have a problem figuring out what each one does.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Hey, one stuff... Why using recursion with a single way cicle??
    Code:
    int factorial(int num)
    {
    
        if (num == 1)
            return 1;
        else
            return num * factorial(num-1);
    }
    I know this is pretty neat code, which one can find in any crapy recursion tutorial, but you could write to make it more eficient (remember that there are stack pointers).
    Code:
    int factorial(int num){
        int ac=1;
        while(num>1)
            ac*=num--;
        return ac;
    }
    Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM