Advertisement
Locoluis

Series

Dec 11th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. /* This assumes that
  5.   * a(1) = 1
  6.   * a(2) = -4 / 6
  7.   * a(3) = 7 / 11
  8.   * and so on.
  9.   * Modify computeTerm() if the second term is wrong.
  10.   */
  11. double computeTerm(int n)
  12. {
  13.     double sign = (n % 2) ? -1.0 : 1.0;
  14.     double numerator = 2.0 - 3.0 * n;
  15.     double denominator = 5.0 * n - 4.0;
  16.     return sign * numerator / denominator;
  17. }
  18.  
  19. double computeSeries(int n)
  20. {
  21.     double sum = 0;
  22.     int i;
  23.     for(i = 1; i <= n; i++) {
  24.         sum += computeTerm(n);
  25.     }
  26.     return sum;
  27. }
  28.  
  29. int main()
  30. {
  31.     int i;
  32.     for(i = 1; i <= 100; i++) {
  33.         printf("%d %.4f\n", i, computeSeries(i));
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement