473,326 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

duchnovsky formula for pi help!

momotaro
357 100+
its about the duchnovsky formula for pi plz find out whats wrong with it!
#include <stdio.h>
Expand|Select|Wrap|Line Numbers
  1. #include <math.h>
  2. #define k1 13591405
  3. #define k2 545140134
  4. #define k3 640320
  5.  
  6. int fact(int x);
  7. main()
  8. {
  9.     long int i, n;
  10.     float pi;
  11.     printf("Enter your and your terms: ");
  12.     scanf("%d", &n);
  13.     for (i=0; i<=n; i++){
  14.         pi=(pow(fact(i), 3)*fact(3*i)*pow(k3, ((3*i+3)/2)))/(((pow(-1, i)*fact(6*i)*k1)+(i*k2)))*12;
  15.     }
  16.     printf("%f", pi);
  17.     sleep(10000);
  18. }
  19.     int fact(int x){
  20.         int i, fact;
  21.         for (i=0; i<=x; i++){
  22.             if (i==0)
  23.                 fact=1;
  24.             else
  25.                 fact*=i;
  26.         }
  27.     return (fact);
  28.     }
Oct 27 '06 #1
5 1606
Banfa
9,065 Expert Mod 8TB
Perhaps you should post all the information about this problem like

1. What is the formula you are actually trying to implement

2. Do you get compile errors? What are they?

3. Do you get run time errors? What are they?

4. Does the program run but produce the wrong result? What result was produced, what result were you expecting? How did you calculate the expected result.
Oct 27 '06 #2
momotaro
357 100+
plz find the formula on this page!
http://www.geocities.com/SiliconValley/Bay/9187/pi.htm
am expecting an approche of pi value!
Oct 27 '06 #3
Banfa
9,065 Expert Mod 8TB
I assume you lack of response to questions 2 and 3 imply a "no" answer.

Perhaps you would like to post a complete reply to question 4.
Oct 27 '06 #4
momotaro
357 100+
Perhaps you should post all the information about this problem like

1. What is the formula you are actually trying to implement?

use this: ?duchnovvsky formula

2. Do you get compile errors? What are they

NO

3. Do you get run time errors? What are they?

NO

4. Does the program run but produce the wrong result? What result was produced, what result were you expecting? How did you calculate the expected result.

YES, it gives a big integer instead the value of pi! and this is the computation part:
Expand|Select|Wrap|Line Numbers
  1. pi=(pow(fact(i), 3)*fact(3*i)*pow(k3, ((3*i+3)/2)))/(((pow(-1, i)*fact(6*i)*k1)+(i*k2)))*12;
Oct 27 '06 #5
Banfa
9,065 Expert Mod 8TB
There are several problems in your implementation of the formula

The formula is

1/PI = SUM(n=0 .. max) Kn

Where Kn is the whole of that large forumla

But you have implemented

PI = SUM(n=0 .. max) 1/Kn

You have assumed that

1/A = X + Y + Z

is equivilent to

A = 1/X + 1/Y + 1/Z

It is not, taking the case of x=2, y=3, z=5

1/A = 2 + 3 + 4

1/A = 9

A = 1/9

A = 1/2 + 1/3 + 1/5

A = 15/30 + 10/30 + 6/30

A = 31/30 = 1 1/30 <> 1/9

You have to calculate 1/PI in the calcualtion loop and then take it's recipricol to find PI.


Next

You are trying to calculate

fact(6*i)

and that fact is done in integer arithmatic. Under integer arithmatic the largest factorial you can calculate is 12! before the arithmatic overflows, since you are using 6*i the largest value of i your code can cope with is 2.

however the forumla actually calls for (6i)!/(3i)! If you can calculate this directly with out calculating the intermediate values of (6i)! and (3i)! then you code will be able to cope with large value of i. This is not has hard as it sounds for the case of i = 2

(6i)! = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12

and

(3i)! = 1 * 2 * 3 * 4 * 5 * 6

Therefore

(6i)!/(3i)! = (1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12) / (1 * 2 * 3 * 4 * 5 * 6)

Canceling out

(6i)!/(3i)! = (7 * 8 * 9 * 10 * 11 * 12)

Unfortunately I have just done the maths and even if you make this alteration you still can't have i>2, so you are going to have to do your factorial calculation in floating point arithmatic. If you do this you can reach values of i up to 28.

You could go higher by writing a function to calculate (6i)!/(3i)! directly without calculating the individual factorials (and I mean it this time).

That is what I have seen so far. I suggest you start by fixing those problems.
Oct 27 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Andreas Schmitt | last post by:
Hi, Sorry for posting in German before, totally forgot about that when I was pasting this in here from another German newsgroup I was writing to, trying to get help I am programming a simple...
0
by: RJN | last post by:
Hi I have a main report and a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the subreport to be evaluated after the formula in the main...
0
by: RJN | last post by:
Hi Sorry for posting this message again. I have a main report and a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the subreport to be...
0
by: rjn | last post by:
Hi I have a main report in which I have inserted a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the subreport to be evaluated after the...
3
by: Mike | last post by:
Hi, I have three tables in the following structure (simplified): Table 1: Containing the customers ------------------------------------------------- create table Customers ( int...
11
by: Brian VanPelt | last post by:
I am not a very experienced VB user, but I was trying to make a form that a user could input a formula for summation. For example, I would like the user to input the beginning and end values of...
5
by: nedian | last post by:
Hello I am student and trying to learn access.I am having problem in making a program can any one help me? i wanted to make 2 tables in access and then create a link.First i am telling u what...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
2
by: dreamer1963 | last post by:
Write a Java program without a graphical user interface that calculates and displays the monthly mortgage payment amount, given the amount of the mortgage, the term of the mortgage, and the interest...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.