Sum of fractions with factorial function. need help.

This is a discussion on Sum of fractions with factorial function. need help. within the C Programming forums, part of the General Programming Boards category; I am trying to write a program which will solve the following expression: (1/2)-(1/2^2!)+(1/2^3!)-(1/2^4!)+(1/2^5!).......+-(1/2^(n-1)!)+-(1/2^n!) I have been trying to debug ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    Sum of fractions with factorial function. need help.

    I am trying to write a program which will solve the following expression:

    (1/2)-(1/2^2!)+(1/2^3!)-(1/2^4!)+(1/2^5!).......+-(1/2^(n-1)!)+-(1/2^n!)

    I have been trying to debug it for a couple of hours now and I can't understand what is wrong. I have tested it with n=5 which should return ~0.26562494 and it is returing 136.

    Here's what I have:
    Code:
    #include <stdio.h>
    
    long int factor(int num1){
        int x;
        long int fact;
        fact = 1;
        for ( x=1; x <= num1; x++ ){
            fact = fact*x;
        }
        return (fact);
    }
    
    float main(){
        int pt, sign, n;
        float frac, som;
        pt = 1;
        sign = -1;
        som = 0;
        printf("Insert your n: \n");
        scanf("%d", &n);
        for (pt = 1; pt <= n; pt++){
            sign = sign * -1;
            frac = sign * ( 1 / ( 2 ^ factor(pt) ) );
            som = som + frac;
        }
        return(som);
    }
    If you happen to find outwhat is wrong, please do share it. If you notice something that could be done in a better way but doesn't have to do with this problem in particular, please share it aswell.

    Thank you very much for your patience and helpfulness

    Tiago Figueiró

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    You are using integer math, so any fraction is dropped. Example 1/2 would be 0.

    Code:
    2 ^ factor(pt) )
    The caret is the XOR operator, not the power.

    Also main must be defined to return an int, not a float.

    Jim
    Last edited by jimblumberg; 10-28-2011 at 03:37 PM.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Thank you for your help.

    So what is the power operator? And what do you mean by "integer" math, since I have defined frac to be a float?

    Please understand I have just started programming and there are some concepts I am not accustomed to.

    thanks

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    There is no power operator. You either do the multiplication or call the pow()

    In the following formula you have used integer math:
    Code:
    frac = sign * ( 1 / ( 2 ^ factor(pt) ) );
    Because all the values to the right of the equal sign are integers integer math will be preformed. In order to force floating point math you will need to in sure that each section of the equation has at least one floating point number. I would suggest unless absolutely necessary, that you do not mix floating point math with integer math. So to fix the above equation use:
    Code:
    float point = factor(pt);
    frac = (float)sign * ( 1.0 /(point * point));
    Jim

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Yaiks!

    Working program:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    long int factor(int num1){
        int x;
        long int fact;
        fact = 1;
        for ( x=1; x <= num1; x++ ){
            fact = fact*x;
        }
        return (fact);
    }
    
    int main(){
        int pt, sign, n;
        float frac, som;
        pt = 1;
        sign = -1.0;
        som = 0.0;
    
        printf("Insert your n: \n");
        scanf("%d", &n);
        for (pt = 1; pt <= n; pt++){
            sign = sign * -1.0;
            float point = factor(pt);
            frac = (float)sign * ( 1.0 / pow(2, point) );
            som = som + frac;
        }
        printf("%f", som);
        return(0);
    }
    Thumbs up for you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (Double) Factorial function efficiency
    By fatum in forum C Programming
    Replies: 2
    Last Post: 10-11-2011, 02:51 AM
  2. Help using factorial function...
    By wco5002 in forum C++ Programming
    Replies: 19
    Last Post: 11-10-2007, 06:42 AM
  3. Factorial function ?
    By koolguysj in forum C Programming
    Replies: 11
    Last Post: 04-01-2005, 10:28 PM
  4. recursive factorial function
    By brianptodd in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2003, 12:56 AM
  5. can't get this function to reduce fractions
    By bruce in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2002, 12:07 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21