How To Estimate Pi Numerically In Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024
  • This video has nothing to do with Good Will Hunting and/or Matt Damon. Just using random sampling and monte carlo to approximate pi.

ความคิดเห็น • 61

  • @XanderGouws
    @XanderGouws 6 ปีที่แล้ว +153

    *draws circle
    "This is a square"

    • @Eniro20
      @Eniro20 5 ปีที่แล้ว +17

      It's an approximation!

    • @J.P.Nery.N.
      @J.P.Nery.N. 5 ปีที่แล้ว +2

      Parker square!

    • @samkonstan5254
      @samkonstan5254 4 ปีที่แล้ว +2

      Subduralempyema the new sinx=x

  • @TRex266
    @TRex266 6 ปีที่แล้ว +29

    I wrote a parallel C++ version and ran this on my Uni's computing cluster across 1000 cores with 5*10^14 sample points in total (took just under 8 hours) and I only got the first 6 digits right. Looks like it doesn't scale as well as you thought :D

  • @someguy9046
    @someguy9046 4 ปีที่แล้ว +16

    "how to estimate pi"
    Say the word "three"
    Job done

  • @saavestro2154
    @saavestro2154 6 ปีที่แล้ว +63

    >"Hi and welcome to numberphile"
    >camera zooms in on the fase of some sperglord already covered in sweat
    >"Today we are going to talk about the number five"
    >sperglord smiles the smile of a thousand suns
    >drops of sweat pouring from his forehead
    >he writes the number five on a paper
    >"The amazing thing about this number is that it is a part of you"
    >camera zooms in on his hand that is soaked in sweat and shaking uncontrolably
    >"As you can see my hand contains five fingers"
    >a tear falls from his eye
    >"I would not even know what to do with myself if not for five"
    >looks at the camera like he is seeing god
    >tears of joy raining down his cheeks
    >In a shaky voice he says:
    >"Without five I couldn't even count to six!"
    >begins to cry

    • @imadmorsli2871
      @imadmorsli2871 4 ปีที่แล้ว

      Based and gay

    • @perigrinus3133
      @perigrinus3133 4 ปีที่แล้ว

      lol i saw seen this meme a while ago. thank you for reminding me of it.

  • @johnchristian5027
    @johnchristian5027 6 ปีที่แล้ว +11

    One of the first things I did when learning how to code in python was approximating pi, its a good exercise! Great Video!

  • @calebwhales
    @calebwhales 6 ปีที่แล้ว +7

    Thanks for this Andrew. As someone learning Python its great to see it's very casual usages, and the lines as thought out in real time. Numerical integration would be great.

  • @lnhostetler
    @lnhostetler 4 ปีที่แล้ว +18

    How I approximate pi in Python:
    import numpy as np
    print(np.pi)
    But sure, I suppose a Monte Carlo approach works too :D

  • @anshsrivastava1059
    @anshsrivastava1059 5 ปีที่แล้ว +10

    I increased N to be around 100000000000000000000000000000 and my computer crashed.

  • @CarlosKoji87
    @CarlosKoji87 6 ปีที่แล้ว +1

    Using the Monte Carlo Method for numerical integration would seem like a really awesome video to make. I've seen it used for the Brachistochrone problem when I was doing an experimental physics project in MatLab a couple years back.

  • @josesierra8793
    @josesierra8793 6 ปีที่แล้ว +1

    This is awesome andrew. I dont even know anything about coding, but i enjoy math, and you make it easy to understand whats going on with your work. Excellent job, man.

  • @rahil_rehan
    @rahil_rehan 5 ปีที่แล้ว +1

    As every point lies in a square, there shouldn't be an if else statement, coz the coordinates get added to square coordinates if they are not in a circle. But all the points do lie in the square, well the output would be right still as you tool float(N) at last computation.

  • @UrsielHauke
    @UrsielHauke 4 ปีที่แล้ว +3

    1:46 we can stop here, that's a good estimation

  • @ratulbanerjee8456
    @ratulbanerjee8456 4 ปีที่แล้ว +1

    I think just use Ramanujan or any other fundamental series approx formulaes and will give digits upto ones will.

  • @natmath2576
    @natmath2576 6 ปีที่แล้ว +4

    That was a real Parker Square

  • @autid
    @autid 6 ปีที่แล้ว +6

    First thought: Monte Carlo?
    3:50 Monte Carlo!

  • @thiagocalomino276
    @thiagocalomino276 2 ปีที่แล้ว

    Cool! Helped me a lot in Numerical Methods at college, thanks. I'm from Brazil!

  • @zoltankurti
    @zoltankurti 6 ปีที่แล้ว +2

    I think generating points in a grid like you had shown in the beginning and than counting them would be more accurate. Also, using only a quarter circle is 4 times faster but has the same precision. Then again, you could write a simple program to integrate sqrt(1-x^2) by divideing it into small rectangles. This could be a part two of this wideo, since it is very useful to see how numerical integration works. You can also better understand the definition of a Riemann integral by this example.

  • @BeLikeNexus
    @BeLikeNexus 4 ปีที่แล้ว +2

    God this mans beard is perfect

  • @sjgabhane
    @sjgabhane 6 ปีที่แล้ว +2

    Great Video!! more videos like these please

  • @ajonautajo
    @ajonautajo 6 ปีที่แล้ว

    Literally did the same project about a year ago. Lent itself to a some statistic analysis to improve the approximation. Good stuff though.

  • @electrikshock2950
    @electrikshock2950 6 ปีที่แล้ว

    You can also get the root of the sum of 6/x^2 and you will get to pi at infinity (or pretty close with a very high number) or this pattern [ (-1)^(x+1)/2x-1 ]

  • @XanderGouws
    @XanderGouws 6 ปีที่แล้ว +14

    Why couldn't you just use a for-loop?
    Also, could you do a vid implementing monte-carlo in python?

    • @AndrewDotsonvideos
      @AndrewDotsonvideos  6 ปีที่แล้ว +3

      for loop, while loop, doesn't matter. And I'm thinking about it

    • @XanderGouws
      @XanderGouws 6 ปีที่แล้ว +5

      Andrew Dotson ye but using a while loop there is _filthy_

    • @Econcrafter
      @Econcrafter 5 ปีที่แล้ว

      @@XanderGouws Exactly my thoughts

    • @andree2062012
      @andree2062012 4 ปีที่แล้ว

      Depends on the general length of the program you do but from an performance point of view, an for loop is much better. Also great video man but your coding style made me cringe a bit :))

  • @coltonhassel2529
    @coltonhassel2529 6 ปีที่แล้ว +2

    Hey just wondering what packages would you recommend installing for these types of things in Jupyter

    • @AndrewDotsonvideos
      @AndrewDotsonvideos  6 ปีที่แล้ว +1

      Can't go wrong with numpy, scipy, sympy, and matplotlib. But I also think those come with anaconda.

  • @expertnonexpert3826
    @expertnonexpert3826 3 ปีที่แล้ว

    is there a book that covers this material. like what "append" means etc? I got "Doing Math With Python" but it seems very basic.

  • @aritraray2501
    @aritraray2501 4 ปีที่แล้ว

    what if I take the random variables range as (0,2) for both x and y??..its not going to give the right value..but i cant understand why

  • @SolathPrime
    @SolathPrime 4 ปีที่แล้ว

    God bless you

  • @Thegreatnintendolets
    @Thegreatnintendolets 5 ปีที่แล้ว

    I think an easier way wouldve been to use the series for arctanx and have it iterate n amount of times to give an approximation

  • @kamehamehaDdragon
    @kamehamehaDdragon 6 ปีที่แล้ว +1

    Great video man, I'm looking foward to learn python, i only know how to code on Java a little, python looks very different but the kind I like.

  • @amandeep9930
    @amandeep9930 3 ปีที่แล้ว

    What about using various infinite series representations of pi?

  • @VskouV
    @VskouV 6 ปีที่แล้ว +2

    "..... Cool!"

  • @kathanshah8305
    @kathanshah8305 3 ปีที่แล้ว +1

    Tl Dw answer is 3

  • @andrewtran6669
    @andrewtran6669 6 ปีที่แล้ว +2

    When did you start learning Python?

    • @AndrewDotsonvideos
      @AndrewDotsonvideos  6 ปีที่แล้ว +5

      Wow only a year ago now that I think about it.

  • @carlosvargas2907
    @carlosvargas2907 6 ปีที่แล้ว

    Great

  • @jessicachaves3666
    @jessicachaves3666 3 ปีที่แล้ว

    thanks :D

  • @principia1372
    @principia1372 3 ปีที่แล้ว

    14:55 CMBR???

  • @nirorit
    @nirorit 4 ปีที่แล้ว

    Why throw darts at random? Can't you just spread the darts evenly to get an absolutely converging sequence?

  • @matron9936
    @matron9936 4 ปีที่แล้ว +1

    If this is a square then pi equals to 4.
    Now i am know how engineers think.

  • @illumiyokai
    @illumiyokai 6 ปีที่แล้ว +3

    First

  • @samkonstan2333
    @samkonstan2333 4 ปีที่แล้ว

    I’ve got another way that’s a lot simpler. print(3)

  • @someaccount3438
    @someaccount3438 5 ปีที่แล้ว +1

    This video is pointless. Everyone knows π = 3.

  • @chillfill4866
    @chillfill4866 4 ปีที่แล้ว

    Stupidest thing I've ever seen. Non deterministicly approaches pi in 2*10^n multiplications where n is each digit of precision. Archimedes did it better 1000s of years ago.

  • @matron9936
    @matron9936 4 ปีที่แล้ว

    Video start:
    This is a square.
    Close video.

  • @ISapTout
    @ISapTout 6 ปีที่แล้ว

    Please, for the love of God, shave that beard. Great video.