View Full Version : Pi calculating script doesnt work?
g0tp0t
09-30-2010, 07:07 PM
var est:extended;i:integer;
function factorial(v:integer):integer;
var i:integer;
begin
for i := v downto 0 do
begin
result := result+i;
end;
end;
begin
est := 0
while not iskeydown(VK_RCONTROL) do
begin
est := ((pow(-1,i)*factorial(6*i)*(13591409+(545140134*i)))/(pow(factorial(i),3)*factorial(3*i)*pow(pow(640320 ,3),i+(1/2))));
i := i+1;
writeln(12*est);
end;
end.
Error:
Compiled succesfully in 31 ms.
Math error at line 16
I dont see what the problem is, here is the formula: =] (https://nrich.maths.org/public/decode.php?tex=%5Cfrac%7B1%7D%7B%5Cpi%7D%3D12%5Csu m_%7Bn%3D0%7D%5E%7B%5Cinfty%7D%5Cfrac%7B%28-1%29%5E%7Bn%7D%286n%29%21%2813591409%2B545140134n% 29%7D%7B%28n%21%29%5E%7B3%7D%283n%29%21%28640320%5 E%7B3%7D%29%5E%7Bn%2B1%2F2%7D%7D)
Nava2
09-30-2010, 08:07 PM
first of all, factorial is multiplication of the numbers consecutively.
Next, you are doing, once again, integer math when you want floats.
Also, you have a complex number in your first pow().
anonymity
09-30-2010, 08:45 PM
How accurately are you trying to calculate pi?
And in how much time?
HyperSecret
10-01-2010, 12:06 AM
your factorial should be:
function Factorial(int number): Integer;
begin
if ((number = 0) or (number = 1)) then
begin
result := 1;
exit;
end else
result := (number * Factorial(number - 1));
end;
OR
function Factorial(number: Integer): Integer;
var temp, i: integer;
begin
temp := 1;
for i := number downto 1 do
temp := temp * i;
end;
I would opt for the 2nd option, the first one using recursion could take awhile with big numbers.
i luffs yeww
10-01-2010, 02:53 AM
^ By the way, there's really no point in doing temp * 1.. for i := number downto 2 do would be slightly faster and do the same thing. :)
Just curious, why downto? I know it doesn't matter, I'm just wondering. I feel like most people would do for i := 2 to number do temp := temp * i;.
g0tp0t
10-04-2010, 05:28 PM
Not sure why i used downto, thats just how i thought of it in my head.
EDIT: nvm, the reason i used downto is because when i do a factorial in my head or on a normal calculator i start from the high number and go down to 1
EDIT: the result:= result+1 works, i tested it before i used it.
EDIT3: new code:
var est,ii:extended;
function factorial(v:integer):integer;
var i:integer;
begin
for i := v downto 1 do
begin
result := result*i;
end;
end;
begin
est := 0
while not iskeydown(VK_RCONTROL) do
begin
est := ((pow(-1,ii)*factorial(int(6*ii))*(13591409+(545140134*ii )))/(pow(factorial(int(ii)),3)*factorial(int(3*ii))*po w(pow(640320,3),ii+(1/2))));
ii := ii+1;
writeln(12*est);
end;
end.
error:
[Error] (17:150): Type mismatch at line 16
Compiling failed.
Another edit:
using a different formula that finds the nth digit of pi:
ar P:string;k:integer;
begin
k:= 1;
while not iskeydown(VK_RCONTROL) do
begin
p := p+FloatToStr((pow(16,-1*k) * ( 4/((8*k)+1) - 2/((8*k)+4) - 1/((8*k)+5) - 1/((8*k)+6) ))) ;
inc(k);
end;
end.
error:
Math error at line 7.
EDIT:btw, the formula is for base16(i think that is hexadecimal) but i am using decimal, so how do i change it to hexadecimal
EDIT: i found out ther is a formula to calculate the nth digit of pi in any base but i cannot find a straight-forward answer, only essays that dont seem to give the formula.
marpis
10-04-2010, 07:13 PM
You're using way too complicated formula, check out the Leibniz' formula:
http://en.wikipedia.org/wiki/Leibniz_formula_for_pi
g0tp0t
10-05-2010, 04:34 PM
lol, the problem with that is that Leibniz's formula converges exponentially slower with each iteration, whereas mine exponentially increases in accuracy. i tested Leibniz's formula in excel and i almost got 3 digits of pi with 1000 iterations. his formula works, but its horrible for convergence
EDIT: i did try to make one using his formula though, it didnt work either:
program new;
var p:double;plus:boolean;i:integer;
begin
i := 0;
plus := true
repeat
if plus then
begin
plus := false;
end else
plus := true;
if plus then
p := p+(1/((2*i)+1))
else
p := p-(1/((2*i)+1));
inc(i);
writeln(p);
wait(100);
until iskeydown(VK_RCONTROL)
end.
the 2i+1 is what i use to generate the denominator and plus is whether to add or subtract that iteration.
EDIT: omg how many times have u guys told me not to use integer division here =\. i did it again and fixed it and now the script works, lol. i still need the other formulas to work though because they are faster.
marpis
10-05-2010, 05:33 PM
lol, the problem with that is that Leibniz's formula converges exponentially slower with each iteration, whereas mine exponentially increases in accuracy. i tested Leibniz's formula in excel and i almost got 3 digits of pi with 1000 iterations. his formula works, but its horrible for convergence
EDIT: i did try to make one using his formula though, it didnt work either:
program new;
var p:double;plus:boolean;i:integer;
begin
i := 0;
plus := true
repeat
if plus then
begin
plus := false;
end else
plus := true;
if plus then
p := p+(1/((2*i)+1))
else
p := p-(1/((2*i)+1));
inc(i);
writeln(p);
wait(100);
until iskeydown(VK_RCONTROL)
end.
the 2i+1 is what i use to generate the denominator and plus is whether to add or subtract that iteration.
EDIT: omg how many times have u guys told me not to use integer division here =\. i did it again and fixed it and now the script works, lol. i still need the other formulas to work though because they are faster.
The speed is not a problem in this case, as your computer can do several million iterations in matter of seconds, and your computer only supports float numbers up to like ~20 digits
XcanadamanX
10-05-2010, 09:27 PM
well speed would become an issue depending on how accurate you want it to be. 10000 terms is accurate to approx 4 decimal places and i think every 0 is good for another decimal place. so 100000000 terms would be good for 8 decimal places...
g0tp0t
10-06-2010, 05:10 PM
lol, sped is exactly the issue, i want to get thousands of digits, not for any real reason. Just for fun.
also each iteration is not a calculation, ther are several calculations per iteration and the computer isnt devoted to the script so it is much slowr than that. i was getting a very slow output, ill add a timeing procedure and post my proggy
g0tp0t
10-07-2010, 11:55 AM
i made the script tell me how many calculations it made per second and i got an average of 60. 60 calculations per second is nothing, im assuming its because pascal is single-threaded.
i definetly need a better algorithm
http://pastebin.com/8Cg5ar3x
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.