Editing Talk:2435: Geothmetic Meandian
Please sign your posts with ~~~~ |
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 284: | Line 284: | ||
My python program: | My python program: | ||
− | + | from math import * | |
− | + | def getMeans(n=list): | |
− | + | n.sort() | |
− | + | mean=sum(n)/len(n) | |
− | + | if len(n)%2==1: | |
− | + | median=n[len(n)//2] | |
− | + | else: | |
− | + | median=(n[1+floor(len(n)/2)]-n[floor(len(n)/2)])/2 | |
− | + | prod=1 | |
− | + | for i in n: | |
− | + | prod*=i | |
− | + | gmean=prod**(1/len(n)) | |
− | + | return [mean,median,gmean] | |
− | + | def gmdn(tol,n=list): | |
− | + | mList=n | |
− | + | mList=getMeans(mList) | |
− | + | while not (isclose(mList[0],mList[1],rel_tol=tol) and isclose(mList[1],mList[2],rel_tol=tol) and isclose(mList[0],mList[2],rel_tol=tol) ) : | |
− | + | mList = getMeans(mList) | |
− | + | return mList[0] | |
− | + | print(gmdn(1e-15,[1,1,2,3,5])) | |
− | |||
It gave me 2.0890579497368584 | It gave me 2.0890579497368584 |