Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: √ ∞ ≠ ≤ ≥ ≈ ⇒ ∈ Δ θ ∴ ∑ ∫ π -

Login

Username

Password

Not registered yet?

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

simron
Real Member

Offline

Python!

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

Code:

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

#2 2009-05-09 12:35:16

bossk171
Power Member

Offline

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.

#3 2009-05-09 12:40:36

simron
Real Member

Offline

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

#4 2009-05-09 15:22:38

MathsIsFun
Administrator

Offline

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

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

simron
Real Member

Offline

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

#6 2009-05-10 03:36:49

bossk171
Power Member

Offline

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.

#7 2009-05-10 04:03:05

bobbym
Administrator

Online

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-10 04:39:14)


In mathematics, you don't understand things. You just get used to them.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

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

simron
Real Member

Offline

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

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

bobbym
Administrator

Online

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.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

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

simron
Real Member

Offline

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

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

bobbym
Administrator

Online

Re: Python!

I agree


In mathematics, you don't understand things. You just get used to them.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

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

bossk171
Power Member

Offline

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-20 05: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.

#13 2009-05-20 09:39:28

simron
Real Member

Offline

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

#14 2009-05-20 09:57:04

bossk171
Power Member

Offline

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.

#15 2009-05-20 14:24:19

bobbym
Administrator

Online

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-21 07:26:40)


In mathematics, you don't understand things. You just get used to them.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

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

gwar
Novice

Offline

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?

Code:

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-22 17:19:23)

#17 2009-06-22 00:53:33

bobbym
Administrator

Online

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-22 03:34:56)


In mathematics, you don't understand things. You just get used to them.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

#18 2009-06-23 16:33:15

gwar
Novice

Offline

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.

Code:

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-23 16:41:35)

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

mathsyperson
Moderator

Offline

Re: Python!

Try this:

Code:

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.

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

bobbym
Administrator

Online

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 23:20:08)


In mathematics, you don't understand things. You just get used to them.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

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

MathsIsFun
Administrator

Offline

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

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

gwar
Novice

Offline

Re: Python!

ok well that makes sense.

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

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

bobbym
Administrator

Online

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 23:48:30)


In mathematics, you don't understand things. You just get used to them.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

#24 2009-06-24 00:33:34

mathsyperson
Moderator

Offline

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.

#25 2009-06-24 01:46:30

bobbym
Administrator

Online

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-24 01:50:49)


In mathematics, you don't understand things. You just get used to them.
I have the result, but I do not yet know how to get it.
All physicists, and a good many quite respectable mathematicians are contemptuous about proof.

Board footer

Powered by FluxBB