Thread: Problem with simple Factorial Estimation

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    4

    Problem with simple Factorial Estimation

    The assignment was to create a program to estimate the factorial of a positive integer using Gosper's formula. I changed it to display intermediate values to find the error, and it looks like root_part is only using the "2n" and not the full "2n + 1/3", but I can't figure out why it's doing that. Reversing them had no effect either. The rest seems accurate given the wrong root_part value.

    Any help would be greatly appreciated.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define PI 3.14159265
    
    int
    	main(void)
    {
    	double  n,
    		root_part,
    		root_solved,
    		n_factorial;
    	printf("Enter a positive integer> ");
    	scanf("%lf", &n);
    
    	root_partial = (1 / 3) + (2 * n);
    	printf("Variable root_part = %f \n", root_part);
    
    	root_solved = sqrt(root_part * PI);
    	printf("Variable root_solved = %f \n", root_solved);
    
    	n_factorial = (pow(n,n) * exp(-n)) * root_solved;
    	printf("%.0f! equals approximately %.5f \n", n, n_factorial);
    
            return(0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1/3 = 0
    1.0/3.0 = .333333~


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4
    Thanks for the quick reply. Fixed everything

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Arthurdent View Post
    Thanks for the quick reply. Fixed everything
    Ahhh... 42.... another Hitchiker's Guide fan!

    Welcome to the forums.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by CommonTater View Post
    Ahhh... 42.... another Hitchiker's Guide fan!

    Welcome to the forums.
    Nah, it's just a coincidense... ( But a good book! )
    Devoted my life to programming...

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    Nah, it's just a coincidense... ( But a good book! )
    Darn... I was so looking forward to the "He's got a TOWEL... run away, run away!" line.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by CommonTater View Post
    Darn... I was so looking forward to the "He's got a TOWEL... run away, run away!" line.
    lol, it's been years since I read this book, yet I still remember it. The author's way of comparing Earth's with Milky Way's bureaucracy was, by my opinion, hilarious! Hehe, the symmenty of events is also outstanding!
    Devoted my life to programming...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    lol, it's been years since I read this book, yet I still remember it. The author's way of comparing Earth's with Milky Way's bureaucracy was, by my opinion, hilarious! Hehe, the symmenty of events is also outstanding!
    Read the book and have the movie... frankly I liked the movie better... "I'm so depressed"...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Probability estimation
    By Mario F. in forum General Discussions
    Replies: 3
    Last Post: 09-18-2009, 08:40 AM
  2. Help with factorial problem please
    By drmk4 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 09:03 PM
  3. factorial problem
    By joker209 in forum C Programming
    Replies: 5
    Last Post: 03-15-2005, 03:36 PM
  4. factorial problem
    By ygfperson in forum C++ Programming
    Replies: 8
    Last Post: 07-25-2003, 09:32 AM
  5. area estimation of graph
    By hei in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 12:23 AM

Tags for this Thread