-5

I want to compute pi using the Spinot's Algorithm of Rabinowitz. It is an infinite sum which consists of two factorials and one power:

The thing is I wrote simple programs for computation of factorials and powers, but I need to combine them in a single for loop. And calculate the sum.

Here's what i did first:

But it is not working. Is it the original idea behind the computation of pi is correct? Is it feasible to compute pi with these series? If yes, what is my mistake here? Any help will be appreciated.Thanks in advance

#include <stdio.h>

int main()
{
  float factorialn,n,sqr,power,factoriald,f;
  factorialn=1;
  n=10;
  power=2;
  factoriald=1;
  for(int i=1;i<=n;i++)
  {
    factorialn*=i;
  }
  sqr=factorialn*factorialn;
  for(int i=1;i<=n;i++)
  {
    power=power*2;
  }
  for(int i=1;i<=2*n+1;i++)
  {
    factoriald=factoriald*i;
  }
  printf("factorialn square is: %10f",sqr);
  printf("Pow of 2 to the n+1: %10f",power);
  printf("Factoriald: %10f",factoriald);

  return 0;
}

Second code:

#include <stdio.h>

int main()  
{
  unsigned long long factorialn,n,sqr,power,factoriald,f;
  double sum;
  factorialn=1;
  n=10;
  power=2;
  factoriald=1;
  for(int i=1;i<=n;i++)
  {
    factorialn*=i;
    power=power*2;
    for(int j=1;j<=2*n+1;j++)
    {
      factoriald=factoriald*j;
    }
    sum+=factorialn*factorialn*power/factoriald;

  }
  printf("summa %1.16lf\n;",sum);  
  return 0;
}
Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42
Shukrullo
  • 11
  • 3
  • 5
    Please post your code directly as plain text, don't use images. – Alex Dec 11 '18 at 10:14
  • Aside: `(i!)^2` is going to be a very large number. – Weather Vane Dec 11 '18 at 10:26
  • Hint: in the inner calculation of `factoriald` don't start `j` at `1`. – pmg Dec 11 '18 at 10:49
  • I did post the plain text @Alex. – Shukrullo Dec 11 '18 at 11:22
  • @pmg Where should j calculation start? – Shukrullo Dec 11 '18 at 11:22
  • Is the problem on choosing variable types or the structure is completely wrong? – Shukrullo Dec 11 '18 at 11:23
  • It should start 1 after where it stopped in the previous loop. Do you want the specific code? – pmg Dec 11 '18 at 11:23
  • What I'm trying to do is to compute both factorials and power. Then arrange them in the fraction and compute the sum of those fractions. But the second factorial is a problem: it has to continue until 2*n+1 while the first factorial and power continues until n. I have no clue how to combine these three computations inside one for loop which has the same n text expression – Shukrullo Dec 11 '18 at 11:38
  • `sum` is not initialized – pmg Dec 11 '18 at 11:40
  • @pmg. You literally saved my nerves; I was very upset about myself. Thank so much for the time you spent on my question even though my post was not so clear. I am thrilled that my first post received so many responses. I didn't expect there is anyone out there who can help me. I really appreciate your help. – Shukrullo Dec 11 '18 at 12:27
  • Glad I could help :) – pmg Dec 11 '18 at 12:28

1 Answers1

0

21! is too large for 64-bit unsigned long long.

I changed everything to long double, initialized sum with 2, and redid the factoriald loop calculation using i.

#include <stdio.h>

int main(void) {
    long double factorialn, n, power, factoriald;
    long double sum = 2;
    factorialn = 1;
    n = 10;
    power = 2;
    factoriald = 1;
    for (int i = 1; i <= n; i++) {
        factorialn *= i;
        power *= 2;
        for (int j = 2 * i; j <= 2 * i + 1; j++) {
            factoriald *= j;
        }
        sum += factorialn * factorialn * power / factoriald;
    }
    printf("summa %1.16Lf\n", sum);
    return 0;
}

https://ideone.com/Fb8c88

pmg
  • 106,608
  • 13
  • 126
  • 198