Thread: [NEWBIE] Trying to find the value of 'e'

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Jakarta
    Posts
    18

    [NEWBIE] Trying to find the value of 'e'

    i'm still new at this, so i really need your help.

    basically i try to find the value of this equation :

    Code:
    e = 1 + 1/1! + 1/2! + 1/3! +.......
    i already find the formula to find final the value of factorial :

    Code:
    while ( value1 > 0) { 
    	
           
    		var1 = var2 * value1--; 
    		result = result * var1; 
    		
    		}
    but i wasn't able to get the code to work. this is how i did so far :

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    main ()
    {
    	int a, b = 0, c = 1, d = 0, e = 0, f = 1, D = 0;
    	
    	while ( a > 0 ) { /* loop for the whole formula
    	
    		do { /* loop for the factorial */
    		
    			D = d;
    			b = c * d--; 
    			f = f * b;
    			} while ( d > 0 );
    		
    		a = 1 / f;
    		e = e + a;
    		D++;
    		}
    		
    		printf ( "%d", e );
    	return 0;
    	}
    where did i messed up ? what should i change ? I already tried diffrent version, and this one is the closest i get... At lease that is what i think....

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1 / f is always zero. Maybe 1.0 / f is what you want; or better yet, make f a double value as well, since an int overflows at less than 20!. (Note also that you'll have to reset f to 1 before every time you start your do-while loop (not every run through the loop itself, but when you compute a different factorial).

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Have you learned about for loops yet, prominababy? It may be a valid alternative to your factorial loop.

    Example:
    Code:
    int main(void)
    {
      int i, f[3] = {0,1,0};
    
      for(i = 0; i < 20; i++)
      {
        f[2] = f[0] + f[1];
        f[0] = f[1];
        f[1] = f[2];
        printf("&#37;d ", f[2]);
      }
    
      return 0;
    }
    Obviously this is a Fibonacci sequence, not a factorial. But since everyone is complaining about answering questions directly as of late, I think its a suitable example of a for loop.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Jakarta
    Posts
    18
    i already try with the looping with for. But the results ends up dapending on how many times i loop it. If i loop it for 3 times than the result is 3 and if i loop it for 4 times than the result is also 4. here is how i done so far, i'm sorry, not very tidy :

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main ()
    {
    
    	int a, b, c, d = 1, f = 0;
    	float g, h = 0, e = 1 ;
    	
    	for ( a = 0; a <= 5; a++ ) {
    	
    		f = b;
    		
    		while ( b > 0 ) {
    		
    			c = d * b--;
    			e = e * c;
    			}
    	
    		g = 1.0 / e;
    		h = h + g;
    		f++;
    		}
    		
    		
    		printf ("the result is %.2f", h);
    		getchar ();
    		return 0;
    	
    	}

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Not very tidy, indeed. At least your loop is much cleaner now, thank you. I saw someone mentioned the overflow issues with doing factorials, why have you not addressed them?

    By the way, b is never initialized nor is it ever reset.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Jakarta
    Posts
    18
    haha ! i found the mistake ! And it's a very annoying one...

    all i have to do was change the f = b into b = f.

    thx for the looping with for tip ! Cheers mate !

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    int factorial(int base, float *result)
    {
      if(base < 13 && result)
      {
        for(*result = 1;base;--base)
          *result *= base;
        return 1;
      }
    
      return 0;
    }
    
    // usage
    if(factorial(b, &e))
    {
      // do stuff
    } else
    {
      fputs("Integer overflow!", stderr);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. could not find -lwsock32.lib
    By thomas_joyee in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 12:28 PM
  3. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM