Discussion about math, puzzles, games and fun. Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ π -¹ ² ³ °

You are not logged in.

- Topics: Active | Unanswered

**simron****Real Member**- Registered: 2006-10-07
- Posts: 237

Here's a Python thing I found on www.nerdparadise.com:

```
import random
def find_pi():
hits = 0
trials = 0
while 1:
x = random.random()
y = random.random()
trials += 1
if x * x + y * y < 1:
hits += 1
if trials % 100 == 1:
print hits * 4.0 / trials
find_pi()
```

Yay Pi!

I'm currently making one that uses the equation

How many people here use Python?

Linux FTW

Offline

**bossk171****Member**- Registered: 2007-07-16
- Posts: 301

I'm interested in using Python, how do I get started?

There are 10 types of people in the world, those who understand binary, those who don't, and those who can use induction.

Offline

**simron****Real Member**- Registered: 2006-10-07
- Posts: 237

There's a good tutorial at http://www.nerdparadise.com/tech/coding/python/beginners1/. And also, Google is your best friend in Python. I've learned an unbelievably large amount of Python just by searching Google for what I don't know.

Linux FTW

Offline

**MathsIsFun****Administrator**- Registered: 2005-01-21
- Posts: 7,560

Easy to understand code. I like that example because it really shows that pi is real. But it has a veeerryyy slow convergence!

I don't use python myself, I tend to use php, javascript and actionscript (for flash).

"The physicists defer only to mathematicians, and the mathematicians defer only to God ..." - Leon M. Lederman

Offline

**simron****Real Member**- Registered: 2006-10-07
- Posts: 237

I ran across Python as I was trying to learn C. They are very similar.

Gosh, I know HTML, JavaScript, Flash, Python, and a little C, and I can't even speak any human languages other than English...

Linux FTW

Offline

**bossk171****Member**- Registered: 2007-07-16
- Posts: 301

I made it as far as here: http://www.python.org/download/ but I'm not sure which to download. I'm using Windows XP.

I know a little C++, but mostly use Actionscript.

There are 10 types of people in the world, those who understand binary, those who don't, and those who can use induction.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,644

Hi boss;

This is what I use for my XP

http://www.python.org/ftp/python/3.0.1/python-3.0.1.msi

*Last edited by bobbym (2009-05-09 06:39:14)*

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

**Online**

**simron****Real Member**- Registered: 2006-10-07
- Posts: 237

Wow, I need an update. I'm currently using Python 2.6...

And the programming tutorial needs only a little programming experience. I think you should be able to go through it with not much trouble.

Linux FTW

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,644

Hi simron;

There is a 3.1 beta at the site also.

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

**Online**

**simron****Real Member**- Registered: 2006-10-07
- Posts: 237

Yeah, I normally only use betas that have been around for a few weeks or so, I'm not much into new ones when I downloaded Firefox 3.1 Beta and it decided to crash every time it started.

Linux FTW

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,644

I agree

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

**Online**

**bossk171****Member**- Registered: 2007-07-16
- Posts: 301

Ahh, help!

I was going through this tutorial: http://docs.python.org/tutorial/index.html

And I quickly realized that I was using 3.0 and the tutorial is written for 2.6. I didn't think that was that big of a deal, but as it turns out, it is. (One example I came across was that the tutorial told me to use the print function without parenthesizes, while the 3.0 required that I type print())

As I get deeper and deeper into the tutorial, it gets worse and worse.

So I guess my question is, should I find a different tutorial, or download a different version?

Thanks in advance.

*Last edited by bossk171 (2009-05-19 07:49:00)*

There are 10 types of people in the world, those who understand binary, those who don't, and those who can use induction.

Offline

**simron****Real Member**- Registered: 2006-10-07
- Posts: 237

Hmm, I haven't had that problem before.

*checks version*

Oh shoot, I use 2.6. I recommend that you install 2.6, but you could just search google when something doesn't work.

(Oh, and I'd recommend installing Python to a flash drive.)

Linux FTW

Offline

**bossk171****Member**- Registered: 2007-07-16
- Posts: 301

simron wrote:

(Oh, and I'd recommend installing Python to a flash drive.)

Why is that? Is that some kind of coder's trick I'm not familiar with, or is it simply for portability?

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,644

Hi bossk171;

Haven't installed 3.0 yet. Shame they haven't kept the documentation up to date. If you install it on a usb drive you might have the advantage of portability, provided it does not alter the registry.

Ok, installed it. Yea, some of the earlier books may be out of date. Nothing to be done about that. The help does provide some good tuts and a whats new section.

*Last edited by bobbym (2009-05-20 09:26:40)*

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

**Online**

**gwar****Member**- Registered: 2009-06-21
- Posts: 3

Hey im probably a little old but i wanna learn more math and i've a passing familiarity with python.

I gave it a shot and it's really slow, can anyone else resolve pi faster?

```
def seq(n):
n = n+2
return [n, n+2]
def findpi():
pi = 0
k = -1
while True:
i = seq(k)
a = (4.0/i[0])-(4.0/i[1])
pi = pi + a
k= k + 4
print pi
findpi()
```

Im pretty sure 3.0 is "the future", aka an experimental version. All the interesing modules I've found are for 2.5+

added more simple

*Last edited by gwar (2009-06-21 19:19:23)*

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,644

Hi gwar;

Your never too old. The problem is with the algorithm that function is using to evaluate pi.

It looks like 4 times the Leibnitz series(1 - 1/3 + 1/5 - 1/7 + 1/9 - ... +) which is way too slow for this task.

*Last edited by bobbym (2009-06-21 05:34:56)*

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

**Online**

**gwar****Member**- Registered: 2009-06-21
- Posts: 3

It was the easiest the use though. Im not sure how to translate this http://en.wikipedia.org/wiki/Bellard%27s_formula

To me it just looks like this, it's obviously not right.

```
n = 0
pi = 1/2^6
while True:
a= -1*n/2^(10*n)*(-2^5/4*(n+1)-1/4*(n+3)+2^8/10*(n+1)-2^6/10*(n+3)-2^2/10*(n+5)-2^2/10*(n+7)+1/10*(n+9))/10*(n+9))
pi+=a
n+=1
print pi
```

*Last edited by gwar (2009-06-22 18:41:35)*

Offline

**mathsyperson****Moderator**- Registered: 2005-06-22
- Posts: 4,900

Try this:

```
n = 0
pi = 0
while True:
a= (-1^n)/2^(10*n)*((-2^5)/(4*n+1)-1/(4*n+3)+(2^8)/(10*n+1)-(2^6)/(10*n+3)-(2^2)/(10*n+5)-(2^2)/(10*n+7)+1/10*(n+9)/(10*n+9))
pi+=a
n+=1
print pi/(2^6)
```

You had things like 4*(n+1), when you wanted 4*n+1.

Also, the 1/2^6 at the start is multiplied by the summation, not added to it.

Why did the vector cross the road?

It wanted to be normal.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,644

Hi gwar;

Even though mathsyperson has corrected that proggie.

Your choice of algorithm is not right.

That formula I believe is a spigot type algorithm.

It will not evaluate pi. It is designed to get the nth binary digit of pi.

*Last edited by bobbym (2009-06-23 01:20:08)*

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

**Online**

**MathsIsFun****Administrator**- Registered: 2005-01-21
- Posts: 7,560

My fave algorithm is the one where you choose two random numbers (x,y) and see if they fall inside the unit circle. You won't get many digits out of it, but it sure is intuitive!

Anyway ... hello gwar, and welcome! Why not just run your first algorithm, see the results, and do some timing, etc. Then research more powerful algorithms and take it from there. Post your results here and keep us entertained

"The physicists defer only to mathematicians, and the mathematicians defer only to God ..." - Leon M. Lederman

Offline

**gwar****Member**- Registered: 2009-06-21
- Posts: 3

ok well that makes sense.

*Last edited by gwar (2009-06-23 01:30:59)*

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,644

Hi gwar;

Go here

http://www.cecm.sfu.ca/organics/papers/borwein/paper/html/node3.html#SECTION00010010000000000000

See that formula it is from the great Ramanujan. It is the best of its type.

The page is maintained by the Borweins,Pflouffe and Bailey, the leaders in this field so

don't be alarmed if it is intimidating.

For simpler ones try here.

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

*Last edited by bobbym (2009-06-23 01:48:30)*

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

**Online**

**mathsyperson****Moderator**- Registered: 2005-06-22
- Posts: 4,900

bobbym wrote:

Your choice of algorithm is not right.

That formula I believe is a spigot type algorithm.

It will not evaluate pi. It is designed to get the nth binary digit of pi.

It does calculate pi, it's just that it does so by calculating its binary digits, then summing them.

Why did the vector cross the road?

It wanted to be normal.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,644

Sorry mathsyperson and gwar;

Yes, reading is fundamental, someday I've got to learn how.

That is a good algorithm and mathsy's program is fine.

Just was a major hallucination on my part, please forgive the error.

*Last edited by bobbym (2009-06-23 03:50:49)*

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

**Online**