[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
My professor has tasked us with finding pi with Python. This
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 19
Thread images: 2
File: python logo.png (14 KB, 121x121) Image search: [Google]
python logo.png
14 KB, 121x121
My professor has tasked us with finding pi with Python.

This is the psudocode we are using as a reference:

'''pseudocode
square of area 4 i.e. side length 2 centered at (0,0)
inscribed circle of radius 1, centered at (0,0)
area of square = 4, area of circle = pi
pi = (area of circle/ area of square) *4'''

Absolutely no one in my class has any prior programming experience, and I was hoping for any possible help for newbies.
>>
>>54315433
>Absolutely no one in my class has any prior programming experience, and I was hoping for any possible help for newbies.
I call bullshit, this reeks of plz do my programming homework for me
>>
>>54315433
import math
print(math.pi)
>>
>>54315656
Goddamn beat me to it
>>
>>54315433
Nobody's doing your homework for you, dumbass.
>>
https://www.youtube.com/watch?v=VJTFfIqO4TU
>>
>>54315433
He's trying to tell you to use the Monte Carlo method. It's pretty inefficient but I made one a while ago.

def monteCarlo(accuracy):
i = 0
inside = 0.0
while i < accuracy:
x = random.random()
y = random.random()
res = (x * x) + (y * y)
if res < 1.0:
inside += 1.0
i += 1
#print(str(inside))
return 4 * inside / accuracy


How this is working (heavily simplified) is that you're creating a square of side length 'x' and a circle inside of it with diameter of 'x'. You then randomly generate a bunch of dots to place on this "image" of a circle and square. The amount of dots which land inside the circle as opposed to inside the square is exactly a quarter of pi.

Also, "no programming experience" is a bad excuse. Just ask. People will make fun of you, but someone ALWAYS helps.
>>
>>54315904
>Just ask. People will make fun of you, but someone ALWAYS helps.

So much this
>>
File: pi.lpg.jpg (79 KB, 1200x900) Image search: [Google]
pi.lpg.jpg
79 KB, 1200x900
If you know calculus it becomes really simple.
>>
>>54315433
pi is the relationship of a circle's to its relational boundary ( graphed as the area of square with side = circle's diameter )

So if you plot a square on a graph and place it in quadrant I, draw a circle inside of it and then find the area of each shape, you can write an equation for the relationship between the two shapes as: Area of Circle / Area of Square

BUT

since you drew the square centered about the origin and not only in quadrant I, your area in quadrant I has only a quarter of your square. To get the rest of the area: Area of Circle / Area of Square * 4.

That should explain it more clearly. One anon helped but he called it the Monte Carlo Method which to me seems like it won't help if it didn't already occur to you. At least this way there won't be any magic numbers.
>>
An update to add some information that was left out:
This is an 11th grade STEM class where all but 2 students have never seen (nor understood) a single line of code. The school also has a very low focus on mathematics, and the teacher expects us to have been taught things that he knows from his highschool experiences. This assignment was also given after about half an hour of python instruction.
>>
This was also the code the teacher started us out with.


import random

'''pseudocode
square of area 4 i.e. side length 2 centered at (0,0)
inscribed circle of radius 1, centered at (0,0)
area of square = 4, area of circle = pi
pi = (area of circle/ area of square) *4'''

thrown = 0
hits = 0
throws = int(input('Enter the number of darts to throw'))
for dart in list(range(0,throws)):
dartx = -1 + 2* random.random()
darty = -1 + 2* random.random()
if (
>>
>>54316957
I'm in same boat almost
>year 12 software development class
>teacher is sport teacher shafted into the wrong position
>not learning code or programming at all
>almost halfway through the year, still learning HTML+CSS
>we will apparently be job ready by end of year according to teacher
Before you call me under age, turned 18 in January
>>
>>54316957
>>54316999
Look up some videos on Monte Carlo, that should help you conceptualize what he's trying to do.

A part from that, the code is pretty beginner level stuff. Do a few online tutorials to get the grips of the basics, and how you're meant to accomplish what you've been assigned.

Also, you can use [ code ] (no spaces) to put formatted code into your post.
>>
>>54316999
Having seen the whole assignment, the first anon that mentioned the monter carlo method was right.

In fact, your pseudocode is all you need to complete the assignment. Find a way to transcribe that whole deal in code and just run it.
>>
>>54317183
My main issue is that I can't figure out how to identify the area of the circle in python. I know you are supposed to check if the "dart" lands within the circle, but I just don't know how to check that.
>>
>>54317783
Thats the part thats reads

If x^2 + y^2 > range

Or something like that. Post a pastebin of the code so i can show you. Your last two attempts at posting it were cut off.
>>
>>54317783
The area of a circle without using pi shouldnbe a given. Else you just use math.pi and PROVE that the relationship, which is not calculated symbollically, is the value for pi. A programming language will never return a symbol. It will return a value that you can then parse yourself.
>>
pi/4 = 2/3 * 4/3 * 4/5 * 6/5 * 6/7 * 8/7 * ...
Or
pi/8 = 1/(1*3) + 1/(5*7) + 1/(9*11) + ...
Thread replies: 19
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.