tags:

views:

108

answers:

2

My primary question is: Is this alot of loops?

while ($decimals < 50000 and $remainder != "0") {
$number = floor($remainder/$currentdivider); //Always round down! 10/3 =3, 10/7 = 1
$remainder = $remainder%$currentdivider; // 10%3 =1, 10%1
$thisnumber = $thisnumber . $number;
$remainder = $remainder . 0; //10
$decimals += 1;
}

Or could I fit more into it? -without the server crashing/lagging.

I'm just wondering, Also is there a more effiecent way of doing the above? (e.g. finidng out that 1/3 = 0.3 to 50,000 decimals.)

Finally:

I'm doing this for a pi formulae the (1 - 1/3 + 1/5 - 1/7 etc.) one,

And i'm wondering if there is a better one. (In php) I have found one that finds pi to 2000 in 4 seconds.

But thats not what I want. I want an infinite series that converges closer to Pi

so every refresh, users can view it getting closer...

But obv. converging using the above formulae takes ALONG time.

Is there any other 'loop' like Pi formulaes (workable in php) that converge faster?

Thanks alot...

+2  A: 

Here you have several formulas for calculating Pi:

http://mathworld.wolfram.com/PiFormulas.html

All of them are "workable" in PHP, like in any other programming language. A different question is how fast they are or how difficult they are to implement.

If the formulas converge faster or slower, it's a Math question, not about programming, so I can't help you. I can tell you that as a rule of a thumb, the less nested loops you put, the faster will be your algorithm (this is a general rule, don't take it as the absolute truth!)

Anyway, since the digits of Pi are known until a certain digit, why don't you copy it into a file and then just index it? That will be extremely fast :)

You can check previous answers to similar questions: http://stackoverflow.com/questions/1788519/calculating-pi-through-php http://stackoverflow.com/questions/3045020/which-is-the-best-formulae-to-find-pi

pakore
One: I don't understand 3/4 of the words on that page :P It's hard to tell which ones are loops without examples.So which ones on that page are loops? :) -And can you show me an example of afew loops :) What is the goal of the formulae... Is it to get closer to infinity, closer to etc...I'm very confused with just 1 link.
Pieman
Every "sigma" letter is a loop. Factorials are loops. Series are loops.
pakore
It would be, but less fun. And as of yet, I don't know a site with Pi to 1 billion. :PAlso nested loops, what are they? :P
Pieman
sigma? factorial? Yeah I sorta understand that. but I don't. Give me a good example on that page of a good algorithm, which loops (alot -ish) and converges fast.
Pieman
Up to 51 billion University of Tokyo They are not publicly available past 4.2B. ftp://pi.super-computing.org/Remember, Google is your friend.
pakore
Nested loops are loops inside loops.
pakore
+1  A: 

Check http://mathworld.wolfram.com/PiIterations.html (taken from the last answer). Those formulaes are using iterations and can therefor be implemented using a loop. You should use google and search for "php implementation xxxxxxx" (where xxxxxx stands for the algorithm name you want to search for).

EDIT: Here is an implementation of Vietas formula using a while-loop in php.

Thariama
Ok thanks......
Pieman