Thread: Finding kth term of a sequence

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    19

    Finding kth term of a sequence

    I want to write an algorithm to find the kth term of a series given by the following recursive formula:
    a_0=1 and a_(k+1)=a_k+1/k!
    My code (part of it responsible for calculating the value of a series) does not want to compile:

    Code:
    float series(int n)
    {
    float F0 = 1;
    float F;
    unsigned int i;
    
    if (n == 0)
    return(1);
    
    for (i = 1; i <= n; i++) 
    {
    F = F0 + 1/factorial(i);
    F0 = F;
    }
    return(F);
    }
    thus my question is why it does not want to work properly and what should i change (for certain factorial function is OK)?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > My code (part of it responsible for calculating the value of a series) does not want to compile:
    Post your actual error message(s), not "it doesn't compile".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    19
    Quote Originally Posted by Salem View Post
    > My code (part of it responsible for calculating the value of a series) does not want to compile:
    Post your actual error message(s), not "it doesn't compile".
    excuse me, i meant that it actualy compiles and after compilation no matter which number (k) i will type in i recieve an answer equal to 2
    Last edited by mmk1234; 12-08-2014 at 02:43 PM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by mmk1234 View Post
    excuse me, i meant that it actualy compiles and after compilation no matter which number (k) i will type in i recieve an answer equal to 2
    You should post a simple, yet complete (i.e. can be compiled), program that exhibits the problem. Then describe what input you're giving, what output you're getting, and what output you're expecting.

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    19
    OK, this is the code. Input i want to have is a natural number, output i want a value of an kth term of a sequence calculated according to the following formula:

    a_0=1 and
    a_(k+1)=a_k+(1/k!)

    In this program, no matter which number i will type in i always recieve an answer equal to 2. How to fix this?

    Code:
    #include "stdafx.h"
    unsigned long long int factorial(unsigned int n)
    {
        if ((n == 0) || (n == 1))
            return(1);
        return(n*factorial(n - 1));
    
    }
    float series(int n)
    {
      float F0 = 1;
      float F;
      unsigned int i;
    
      if (n == 0)
        return(1);
    
      for (i = 1; i <= n; i++) 
      {
        F = F0 + 1/factorial(i);
        F0 = F;
      }
    return(F);
    }
    
    int _tmain(int argc, _TCHAR* argv[])    
    {
        int k;
        printf_s("give number:\n");
        scanf_s("%d", &k);
        printf("series(%d) = %f\n", k, series(k));
        return(0);
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 1/factorial(i);
    Integer division truncates to zero.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2014
    Posts
    19
    but how to fix it? i have changed all of the variables to float type, what more can i do?
    edit:
    apologise i think that putting (float)solved problem
    Last edited by mmk1234; 12-08-2014 at 03:45 PM.

  8. #8
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Since 1 is an integer, and factorial(i) returns a long long int, the result of 1/factorial(i) is a long long int.

    You can rely on implicit conversion rules for C arithmetic, and use 1.0f (for floating-point one) or 1.0 (for double-precision one), instead. The compiler will then implicitly convert the divisor to a floating-point type. Personally, I recommend using 1.0f / (float)factorial(i) instead, i.e. do the casts explicitly. I think it is easier for us humans to immediately see which precision/rules are used for the computation that way. Others may disagree.

    You should reconsider your types, however. Most current architectures use IEEE-754 binary32 for float, and binary64 for double; the latter would give you a larger range of valid values. In general, double type has at least the same range and precision as float, so for large or precise results, I'd always prefer double.

    I'd personally suggest keeping n in factorial() as an integer, but do the calculation and return the result using the double type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Question about finding nth term of a sequence
    By mmk1234 in forum C Programming
    Replies: 4
    Last Post: 12-07-2014, 06:16 PM
  2. Printing to the nth term
    By greenj2 in forum C Programming
    Replies: 5
    Last Post: 07-16-2013, 09:53 AM
  3. Data from term window
    By linuxtest in forum C Programming
    Replies: 8
    Last Post: 03-29-2009, 10:51 PM
  4. Where did the term FOO come from?
    By Dragoon_42 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-27-2003, 05:03 PM