its about the duchnovsky formula for pi plz find out whats wrong with it!
#include <stdio.h> - #include <math.h>
-
#define k1 13591405
-
#define k2 545140134
-
#define k3 640320
-
-
int fact(int x);
-
main()
-
{
-
long int i, n;
-
float pi;
-
printf("Enter your and your terms: ");
-
scanf("%d", &n);
-
for (i=0; i<=n; i++){
-
pi=(pow(fact(i), 3)*fact(3*i)*pow(k3, ((3*i+3)/2)))/(((pow(-1, i)*fact(6*i)*k1)+(i*k2)))*12;
-
}
-
printf("%f", pi);
-
sleep(10000);
-
}
-
int fact(int x){
-
int i, fact;
-
for (i=0; i<=x; i++){
-
if (i==0)
-
fact=1;
-
else
-
fact*=i;
-
}
-
return (fact);
-
}
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.
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.
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:
- pi=(pow(fact(i), 3)*fact(3*i)*pow(k3, ((3*i+3)/2)))/(((pow(-1, i)*fact(6*i)*k1)+(i*k2)))*12;
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
by: Mike |
last post by:
Hi,
I have three tables in the following structure (simplified):
Table 1: Containing the customers
-------------------------------------------------
create table Customers
(
int...
|
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...
|
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...
|
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 ...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
|
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...
| |