Math Is Fun Forum

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

You are not logged in.

#1 2009-05-08 10:24:23

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

Python!

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

#2 2009-05-08 14:35:16

bossk171
Member
Registered: 2007-07-16
Posts: 305

Re: Python!

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

#3 2009-05-08 14:40:36

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

Re: Python!

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

#4 2009-05-08 17:22:38

MathsIsFun
Administrator
Registered: 2005-01-21
Posts: 7,713

Re: Python!

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

#5 2009-05-09 01:49:47

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

Re: Python!

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

#6 2009-05-09 05:36:49

bossk171
Member
Registered: 2007-07-16
Posts: 305

Re: Python!

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

#7 2009-05-09 06:03:05

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Python!

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.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#8 2009-05-09 10:15:00

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

Re: Python!

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

#9 2009-05-09 10:59:48

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Python!

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.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#10 2009-05-10 07:48:24

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

Re: Python!

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

#11 2009-05-10 21:04:04

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Python!

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.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#12 2009-05-19 07:45:07

bossk171
Member
Registered: 2007-07-16
Posts: 305

Re: Python!

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

#13 2009-05-19 11:39:28

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

Re: Python!

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

#14 2009-05-19 11:57:04

bossk171
Member
Registered: 2007-07-16
Posts: 305

Re: Python!

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?


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

Offline

#15 2009-05-19 16:24:19

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Python!

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.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#16 2009-06-21 01:59:35

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

Re: Python!

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

#17 2009-06-21 02:53:33

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Python!

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.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#18 2009-06-22 18:33:15

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

Re: Python!

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

#19 2009-06-22 23:42:27

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

Re: Python!

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

#20 2009-06-23 01:16:37

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Python!

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.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#21 2009-06-23 01:25:53

MathsIsFun
Administrator
Registered: 2005-01-21
Posts: 7,713

Re: Python!

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 smile


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

Offline

#22 2009-06-23 01:28:56

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

Re: Python!

ok well that makes sense.

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

Offline

#23 2009-06-23 01:47:37

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Python!

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.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#24 2009-06-23 02:33:34

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

Re: Python!

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

#25 2009-06-23 03:46:30

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Python!

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.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

Board footer

Powered by FluxBB